【发布时间】:2018-07-18 20:54:39
【问题描述】:
我是 python 新手。也从不使用 JSON。我有一个 json 文件,由于第 8 行中缺少“,”而无法打开。所以如何用 python 解决这个问题。我知道如何使用 python json lib 处理 JSON 文件中的缺失值。
我将 python3 与 ipython 一起使用
我使用的python脚本:
import json
with open('name_dob_height.json') as json_file:
d = json.load(json_file)
print(d)
最后一行错误消息:
JSONDecodeError: Expecting ',' delimiter: line 8 column 3 (char 109)
json_file =
[
{
"First_name": "Nathan",
"Last_name": "Astle",
"Date_of_birth": "1971-05-18",
"Height": "155"
}{
"First_name": "Nathan",
"Last_name": "Astle",
"Date_of_birth": "1971-05-19",
"Height": "155"
},
{
"First_name": "Arthur",
"Last_name": "Harrison",
"Date_of_birth": "1982-01-29",
"Height": "168"
},
{
"First_name": "Cameron",
"Last_name": "White",
"Date_of_birth": "1982-01-29",
"Height": "170"
},
{
"First_name": "Steve",
"Last_name": "Finn",
"Date_of_birth": "1971-05-18",
"Height": "189"
},
{
"First_name": "Elliot",
"Last_name": "Luca",
"Date_of_birth": "1971-05-18",
"Height": "172"
},
{
"First_name": "Methews",
"Last_name": "Haiden",
"Date_of_birth": "1971-05-18",
"Height": "165"
},
{
"First_name": "John",
"Last_name": "Smith",
"Date_of_birth": "1971-05-18",
"Height": "invalid string"
},
{
"First_name": "Daniel",
"Last_name": "Clerk",
"Date_of_birth": "1971-05-18",
"Height": "190"
},
{
"First_name": "Allan",
"Last_name": "Hernandez",
"Date_of_birth": "1981-01-19",
"Height": "178"
},
{
"First_name": "Harley",
"Last_name": "Smith",
"Date_of_birth": "1988-05-21",
"Height": "185"
},
{
"First_name": "Nathaniel",
"Last_name": "Stewert",
"Date_of_birth": "1980-08-10",
"Height": "125"
},
{
"First_name": "Michael",
"Last_name": "Johnson",
"Date_of_birth": "1990-03-26",
"Height": "152"
}
]
【问题讨论】:
-
对于 json 文件,您必须使用 json 模块。谷歌一下,你会找到的。
-
@Narendra 看起来 OP 已经在使用 json 模块了
-
第一个对象之后,JSON文件中没有逗号。请添加逗号它会解决您的问题。
-
@kvk30 我可以手动执行此操作。我用python程序去了
-
@NajeebChoudhary 具有正确的 Json 格式,这是不可能的。
标签: python json python-3.x ipython jupyter-notebook