gpt4 book ai didi

python - 值错误: No JSON object could be decoded for a valid JSON file

转载 作者:行者123 更新时间:2023-12-01 09:28:18 25 4
gpt4 key购买 nike

我有一个大小为 109 MGB 的有效 JSON 文件,具有以下格式表示。

[{"sensorType":"Radarcape","sensorLatitude":{"double":51.758894},"sensorLongitude":{"double":-1.256654},"sensorAltitude":{"double":200.0},"timeAtServer":1.429617600000027E9,"timeAtSensor":null,"timestamp":{"double":9.68628281E8},"rawMessage":"8d4008f999059a90c09719d7b430","sensorSerialNumber":13020235,"RSSIPacket":{"double":52.0},"RSSIPreamble":null,"SNR":null,"confidence":null}
,{"sensorType":"Radarcape","sensorLatitude":{"double":51.758894},"sensorLongitude":{"double":-1.256654},"sensorAltitude":{"double":200.0},"timeAtServer":1.429617600001544E9,"timeAtSensor":null,"timestamp":{"double":9.74843812E8},"rawMessage":"8f406a9a5913d2d51fbe8ab14fdf","sensorSerialNumber":13020235,"RSSIPacket":{"double":72.0},"RSSIPreamble":null,"SNR":null,"confidence":null}]

读取JSON文件的简单代码:

import json

with open ("jsonfile.txt",'r') as f:

j=json.load(f)

我正在尝试解析它以将其转换为 SQLite 格式。但是,每次我收到以下消息错误时。

Traceback (most recent call last):
File "C:/Users/PycharmProjects/jsontosqlite/test.py", line 5, in <module>
a=json.loads("newfile.json")
File "C:\Python27\lib\json\__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "C:\Python27\lib\json\decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

最佳答案

如果我将问题中显示的 JSON 数据(准确地)放入 jsonfile.txt 文件中并运行它,则以下内容对我有用:

import json

with open ("jsonfile.txt", 'r') as f:
j = json.loads(f.read())

print(json.dumps(j, indent=4))

输出:

[
{
"sensorType": "Radarcape",
"sensorLatitude": {
"double": 51.758894
},
"sensorLongitude": {
"double": -1.256654
},
"sensorAltitude": {
"double": 200.0
},
"timeAtServer": 1429617600.000027,
"timeAtSensor": null,
"timestamp": {
"double": 968628281.0
},
"rawMessage": "8d4008f999059a90c09719d7b430",
"sensorSerialNumber": 13020235,
"RSSIPacket": {
"double": 52.0
},
"RSSIPreamble": null,
"SNR": null,
"confidence": null
},
{
"sensorType": "Radarcape",
"sensorLatitude": {
"double": 51.758894
},
"sensorLongitude": {
"double": -1.256654
},
"sensorAltitude": {
"double": 200.0
},
"timeAtServer": 1429617600.001544,
"timeAtSensor": null,
"timestamp": {
"double": 974843812.0
},
"rawMessage": "8f406a9a5913d2d51fbe8ab14fdf",
"sensorSerialNumber": 13020235,
"RSSIPacket": {
"double": 72.0
},
"RSSIPreamble": null,
"SNR": null,
"confidence": null
}
]

关于python - 值错误: No JSON object could be decoded for a valid JSON file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50180707/

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