gpt4 book ai didi

python - Python无法打开JSON文件,给出JSONDecodeError

转载 作者:行者123 更新时间:2023-12-03 00:57:09 24 4
gpt4 key购买 nike

我想在项目中使用Python打开JSON文件,但是不断出现以下错误:

json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

这是代码:
import json
with open("../data.txt") as json_file:
data = json.load(json_file)

我有一个非常简单的文本文件,其中包含JSON格式的数据。这是 data.txt文件:
{
"data": [
{
"day": "22/04/2020 15:35",
"viewcount": "1"
},
{
"day": "22/04/2020 20:51",
"viewcount": "2"
}
]
}

最佳答案

我已经尝试了您的源代码和可见的JSON数据,它运行起来没有任何问题。

我建议以二进制形式检查文件的内容,例如通过使用诸如hexdump之类的实用程序来查看它是如何开始的:

$ hexdump data.txt 
0000000 0a7b 2020 2020 6422 7461 2261 203a 5b20
0000010 200a 2020 2020 2020 2020 2020 2020 2020
0000020 2020 0a7b 2020 2020 2020 2020 2020 2020
...

或者使用 file实用程序来检查编码,如以下文章中所述: https://unix.stackexchange.com/questions/11602/how-can-i-test-the-encoding-of-a-text-file-is-it-valid-and-what-is-it

关于python - Python无法打开JSON文件,给出JSONDecodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61383272/

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