I can't parse a json file with python -
first post here.
i've been using python while now, i'm stuck simple case.
i want parse json file simplejson module: here code:
import simplejson open('myjsontest.json', 'r') data_file: print data_file.read() session = simplejson.load(data_file, strict=false)
and here json file named myjsontest.json:
[ { "test1": 1, "test2": 2, "test3": 3, "test4": 4 } ]
the json file in same folder python file.
i got result:
[ { "test1": 1, "test2": 2, "test3": 3, "test4": 4 } ] traceback (most recent call last): file ".\test.py", line 8, in <module> session = simplejson.load(data_file, strict=false) file "c:\users\gordon\anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\__init__.py", line 459, in loa d use_decimal=use_decimal, **kw) file "c:\users\gordon\anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\__init__.py", line 533, in loa ds return cls(encoding=encoding, **kw).decode(s) file "c:\users\gordon\anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\decoder.py", line 370, in deco de obj, end = self.raw_decode(s) file "c:\users\gordon\anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\decoder.py", line 400, in raw_ decode return self.scan_once(s, idx=_w(s, idx).end()) file "c:\users\gordon\anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\scanner.py", line 127, in scan _once return _scan_once(string, idx) file "c:\users\gordon\anaconda2\lib\site-packages\simplejson-3.8.1-py2.7.egg\simplejson\scanner.py", line 87, in _scan _once raise jsondecodeerror(errmsg, string, idx) simplejson.scanner.jsondecodeerror: expecting value: line 1 column 1 (char 0)
i think think may have problem in os/python setup? python 32b 2.7.11 installed anaconda on windows7 64b.
thanks if can help.
once read
file, stream @ end , cannot read anymore. code should work if remove print data_file.read()
statement, or .seek()
beginning of file afterwards.
Comments
Post a Comment