Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在编码json之后,在python中读取它_Python_Json - Fatal编程技术网

在编码json之后,在python中读取它

在编码json之后,在python中读取它,python,json,Python,Json,我有一个json文件,在使用以下代码时加载失败: indices_json_path = 'file.json' with open(indices_json_path) as json_data: d = json.load(json_data) 但是如果我在notepad++中手动打开这个文件并单击encode=>encodeUTF-8,然后保存文件并重新运行代码,这样一切都可以正常工作。 我想避免手工修理。有什么帮助吗?我可能猜这是一个编码错误,请尝试: import io wi

我有一个json文件,在使用以下代码时加载失败:

indices_json_path = 'file.json'
with open(indices_json_path) as json_data:
    d = json.load(json_data)
但是如果我在notepad++中手动打开这个文件并单击encode=>encodeUTF-8,然后保存文件并重新运行代码,这样一切都可以正常工作。
我想避免手工修理。有什么帮助吗?

我可能猜这是一个编码错误,请尝试:

import io
with io.open(indices_json_path,'r',encoding='utf8') as json_data:
    d = json.load(json_data)

这就解决了问题:

with io.open(indices_json_path,'r', encoding='UTF-16-LE') as json_data:
    d = json.load(json_data)

会出现什么错误?obj,end=self.scan_once(s,idx)json.decoder.jsondeCoderror:应使用双引号括起属性名:第1行第2列(char 1),因此它会将您“用手”而不是json模块写入文件)