导入json文件报错,TypeError expected string or buffer

Posted pinpin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了导入json文件报错,TypeError expected string or buffer相关的知识,希望对你有一定的参考价值。

导入json文件报错,TypeError expected string or buffer

原因:用字符串赋值后,python会把双引号转换为单引号

import json

data = [{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}]
print(type(data),data)

执行结果:

<class ‘list‘> [{‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘d‘: 4, ‘e‘: 5}]

但是了,json是不支持单引号的。可以用下面的方法转换

json_string=json.dumps(s)

python_obj=json.loads(json_string)

实例:

import json

data = [{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}]
json_string = json.dumps(data) #dumps序列化为str,所以保证了双引号没有变为单引号
python_obj=json.loads(json_string) #oads反序列化,所以与原data相同
print(type(json_string),json_string)
print(type(python_obj),python_obj)

执行结果:

<class ‘str‘> [{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5}]
<class ‘list‘> [{‘a‘: 1, ‘b‘: 2, ‘c‘: 3, ‘d‘: 4, ‘e‘: 5}]

以上是关于导入json文件报错,TypeError expected string or buffer的主要内容,如果未能解决你的问题,请参考以下文章

Oracle之数据泵导入/导出数据

如何在plsql 执行导入导入语句: 在dos下能直接执行exp 和imp语句,但plsql下执行报错?

项目中遇到Uncaught TypeError: Converting circular structure to JSON报错问题

报错TypeError: Cannot read property 'range' of null

js为啥会报错Uncaught TypeError: Cannot read property 'style' of undefined?

exp img 根据数据库用户导入导出数据库表