【发布时间】:2016-12-08 04:45:58
【问题描述】:
这是我的 JSON 文件的内容
cat ./myfile.json
{u'Records': [{u'eventVersion': u'2.0', }]}
如何读取这个 JSON 文件?
我尝试使用以下代码读取文件,
def Read_json_file(jsonFile):
jsonDy = {}
if os.path.exists(jsonFile):
with open(jsonFile, 'rt') as fin:
jsonDy = json.load(fin)
else:
print("JSON file not available ->",
jsonFile)
sys.exit(1)
print("jsonDy -> ", jsonDy)
但出现以下错误,
Traceback (most recent call last):
File "a.py", line 125, in <module>
Main()
File "a.py", line 18, in Main
content = Read_json_file(eventFile)
File "a.py", line 44, in Read_json_file
jsonDy = json.load(fin)
File "/usr/lib64/python2.7/json/__init__.py", line 290, in load
**kw)
File "/usr/lib64/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib64/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib64/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)
我理解的是这里u' 代表unicode 表示法,但不知道如何读取这个文件
PS:我使用的是 Python 2.7
【问题讨论】:
-
改用
json.loads()。
标签: python json python-2.7 unicode python-unicode