本文介绍了编码后读取python中的json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用以下代码时,我有一个无法加载的json文件:
I have a json file that fails loading when I use the following code:
indices_json_path = 'file.json'
with open(indices_json_path) as json_data:
d = json.load(json_data)
但是,如果我在notepad ++中手动打开此文件并单击"encode => encode UTF-8",则保存该文件并重新运行代码,以便一切正常.我想避免手动维修.有帮助吗?
But if I manually open this file in notepad++ and click encode=>encode UTF-8, then save the file and re-run the code so everything works well.I would like to avoid manual repair. Any help?
推荐答案
我可能会认为这是编码错误,请尝试:
I would probably guess it is an encoding error, try:
import io
with io.open(indices_json_path,'r',encoding='utf8') as json_data:
d = json.load(json_data)
这篇关于编码后读取python中的json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!