gpt4 book ai didi

python - 将 .json 加载到 python 中; Unicode解码错误

转载 作者:行者123 更新时间:2023-11-28 20:21:41 24 4
gpt4 key购买 nike

我试图将一个 json 文件加载到 python 中,但没有成功。在过去的几个小时里,我一直在谷歌搜索解决方案,但似乎无法加载它。我尝试使用适用于所有人的相同 json.load('filename') 函数加载它。我不断得到:“UnicodeDecodeError:‘utf8’编解码器无法解码位置 124 中的字节 0xc2:无效的连续字节”

这是我使用的代码

import json
json_data = open('myfile.json')
for line in json_data:
data = json.loads(line) <--I get an error at this.

这是我文件中的示例行

{"topic":"security","question":"Putting the Biba-LaPadula Mandatory Access Control Methods to Practise?","excerpt":"Text books on database systems always refer to the two Mandatory Access Control models; Biba for the Integrity objective and Bell-LaPadula for the Secrecy or Confidentiality objective.\n\nText books ...\r\n        "}

如果这似乎对我搜索过的每个示例中的每个人都有效,那我的错误是什么?

最佳答案

你试过吗:

json.loads(line.decode("utf-8"))

此处提出类似问题:UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2

编辑:如果上述方法不起作用,

json.loads(line.decode("utf-8","ignore"))

会。

关于python - 将 .json 加载到 python 中; Unicode解码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27340542/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com