我正在加载包含在文本文件中的以下json:
{
"data sources" : [
"http://www.gcmap.com/"
] ,
"metros" : [
{
"code" : "SCL" ,
"name" : "Santiago" ,
"country" : "CL" ,
"continent" : "South America" ,
"timezone" : -4 ,
"coordinates" : {"S" : 33, "W" : 71} ,
"population" : 6000000 ,
"region" : 1
} , {
"code" : "LIM" ,
"name" : "Lima" ,
"country" : "PE" ,
"continent" : "South America" ,
"timezone" : -5 ,
"coordinates" : {"S" : 12, "W" : 77} ,
"population" : 9050000 ,
"region" : 1
} ]}
然后我将此文件放在与将打开它的python文件相同的目录中,并使用以下代码:
import json
json_file = open('json.txt')
data = json.load(json_file)
json_file.close()
print (data)
然而,这给了我一个错误:
Traceback (most recent call last):
File "/Users/tylerharrington/Desktop/workspace/Assignment2/src/cs242assignment2/UserInterface.py", line 11, in <module>
data = json.load(json_file)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/__init__.py", line 264, in load
parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/__init__.py", line 309, in loads
return _default_decoder.decode(s)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/decoder.py", line 352, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/json/decoder.py", line 368, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name enclosed in double quotes: line 1 column 1 (char 1)
这是否表示python文件出错或代码出错?
答案 0 :(得分:0)
Expecting property name enclosed in double quotes: line 1 column 1 (char 1)
您的文件可能在左大括号{
使用od
转储文件以检查文件内容,或者在Windows中使用十六进制编辑器检查文件
或者只需使用您的文件内容
运行以下代码段with open('json.txt') as fin:
for line in fin:
print [hex(ord(e)) for e in line]
这将让您对违规字符
有一个全面的了解或只是
hex(ord(open('test.txt').read(2)[1]))