在 Python 中加载带有单引号和花括号的 JSON 文件
Load JSON file with single-quotes and curly {} braces in Python
我正在尝试在 python 中加载 json 文件。我的 json 文件如下所示:
{'awesomeness': 2.5, 'party': 'Republican', 'Age': 50, 'State': 'California', 'Ideology': 0.5,
'time': {'day': 20, 'mon': 2, 'sec': 3}, 'overall': 9.5, 'review': 'Best Senator ever\tPretty balanced
view.\tNot sure if he can get reelected'}
{'awesomeness': 0.5, 'party': 'Republican', 'Age': 70, 'State': 'New York', 'Ideology': 0.8,
'time': {'day': 25, 'mon': 8, 'sec': 31}, 'overall': 5.5, 'review': 'NA'}
这是我的代码。
with open("senator.json") as json_file:
data = json.load(json_file)
但是我得到了以下错误,
File "<stdin>", line 1, in <module>
File "<string>", line 2, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load
**kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
为什么会出现此错误以及如何加载此文件?感谢您的帮助!
JSON 字符串用双引号分隔,不是单引号。您的输入不正确 JSON。
您的 json 文件格式不正确 json...我认为应该是这样的
{'awesomeness': 2.5, 'party': 'Republican', 'Age': 50, 'State': 'California', 'Ideology': 0.5,'time': {'day': 20, 'mon': 2, 'sec': 3}, 'overall': 9.5, 'review': 'Best Senator ever\tPretty balanced w.\tNot sure if he can get reelected','awesomeness': 0.5, 'party': 'Republican', 'Age': 70, 'State': 'New York', 'Ideology': 0.8,'time': {'day': 25, 'mon': 8, 'sec': 31}, 'overall': 5.5, 'review': 'NA'}
你应该阅读 file.Syntax 阅读是
with open("senator.json","r") as json_file:
data = json.load(json_file)
正如其他人已经指出的那样,您的 JSON 是非法的。这就是为什么 Python JSON 解析器完全崩溃的原因。您的 JSON 应如下所示:
[
{
"awesomeness": 2.5,
"party": "Republican",
"Age": 50,
"State": "California",
"Ideology": 0.5,
"time": {
"day": 20,
"mon": 2,
"sec": 3
},
"overall": 9.5,
"review": "Best Senator ever \t Pretty balanced view. \t Not sure if he can get reelected"
},
{
"awesomeness": 0.5,
"party": "Republican",
"Age": 70,
"State": "NewYork",
"Ideology": 0.8,
"time": {
"day": 25,
"mon": 8,
"sec": 31
},
"overall": 5.5,
"review": "NA"
}
]
注意字符串的双引号而不是单引号(在 JSON 中表示字符串是非法的)并将两个对象 {...}
包裹在方括号中,因此它们在 array
.
后者是必需的,因为 JSON 是 分层的 。您发布的 JSON 代码只有两个并排的对象,没有任何外部结构。在那种情况下,如果您因此离开方括号(无数组),那么 JSON 解析器将在您的第一个对象之后突然遇到一个逗号 ,
而实际上它期望的是 EOF
.
现在,如果您再次尝试 运行 您的代码:
with open('senator.json', 'r') as json_file:
data = json.load(json_file)
您应该能够毫无错误地读出文件。
我找到了另一种方法来处理这个单引号问题。我使用 for
循环和 eval
来读取这个文件。
这是我使用的代码。
def getdata(file):
for l in open(file):
yield eval(l)
thedata = list(getdata('filename.json')
谢谢大家告诉我问题!
我正在尝试在 python 中加载 json 文件。我的 json 文件如下所示:
{'awesomeness': 2.5, 'party': 'Republican', 'Age': 50, 'State': 'California', 'Ideology': 0.5,
'time': {'day': 20, 'mon': 2, 'sec': 3}, 'overall': 9.5, 'review': 'Best Senator ever\tPretty balanced
view.\tNot sure if he can get reelected'}
{'awesomeness': 0.5, 'party': 'Republican', 'Age': 70, 'State': 'New York', 'Ideology': 0.8,
'time': {'day': 25, 'mon': 8, 'sec': 31}, 'overall': 5.5, 'review': 'NA'}
这是我的代码。
with open("senator.json") as json_file:
data = json.load(json_file)
但是我得到了以下错误,
File "<stdin>", line 1, in <module>
File "<string>", line 2, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 290, in load
**kw)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/decoder.py", line 381, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 1 column 2 (char 1)
为什么会出现此错误以及如何加载此文件?感谢您的帮助!
JSON 字符串用双引号分隔,不是单引号。您的输入不正确 JSON。
您的 json 文件格式不正确 json...我认为应该是这样的
{'awesomeness': 2.5, 'party': 'Republican', 'Age': 50, 'State': 'California', 'Ideology': 0.5,'time': {'day': 20, 'mon': 2, 'sec': 3}, 'overall': 9.5, 'review': 'Best Senator ever\tPretty balanced w.\tNot sure if he can get reelected','awesomeness': 0.5, 'party': 'Republican', 'Age': 70, 'State': 'New York', 'Ideology': 0.8,'time': {'day': 25, 'mon': 8, 'sec': 31}, 'overall': 5.5, 'review': 'NA'}
你应该阅读 file.Syntax 阅读是
with open("senator.json","r") as json_file:
data = json.load(json_file)
正如其他人已经指出的那样,您的 JSON 是非法的。这就是为什么 Python JSON 解析器完全崩溃的原因。您的 JSON 应如下所示:
[
{
"awesomeness": 2.5,
"party": "Republican",
"Age": 50,
"State": "California",
"Ideology": 0.5,
"time": {
"day": 20,
"mon": 2,
"sec": 3
},
"overall": 9.5,
"review": "Best Senator ever \t Pretty balanced view. \t Not sure if he can get reelected"
},
{
"awesomeness": 0.5,
"party": "Republican",
"Age": 70,
"State": "NewYork",
"Ideology": 0.8,
"time": {
"day": 25,
"mon": 8,
"sec": 31
},
"overall": 5.5,
"review": "NA"
}
]
注意字符串的双引号而不是单引号(在 JSON 中表示字符串是非法的)并将两个对象 {...}
包裹在方括号中,因此它们在 array
.
后者是必需的,因为 JSON 是 分层的 。您发布的 JSON 代码只有两个并排的对象,没有任何外部结构。在那种情况下,如果您因此离开方括号(无数组),那么 JSON 解析器将在您的第一个对象之后突然遇到一个逗号 ,
而实际上它期望的是 EOF
.
现在,如果您再次尝试 运行 您的代码:
with open('senator.json', 'r') as json_file:
data = json.load(json_file)
您应该能够毫无错误地读出文件。
我找到了另一种方法来处理这个单引号问题。我使用 for
循环和 eval
来读取这个文件。
这是我使用的代码。
def getdata(file):
for l in open(file):
yield eval(l)
thedata = list(getdata('filename.json')
谢谢大家告诉我问题!