json error in loading from file (python) -


i'm learning how use json in python , have encountered problem: next 2 paragraphs run separately same directory:

x=[1,-1,[1]] import json f=open('states','w') f.close() f=open('states','r+') json.dump(x,f) json.dump(x,f) f.close()  f=open('states','r+') y=json.load(f) f.close() print y 

the first part seems run fine when run second part error occurs:

valueerror                                traceback (most recent call last) <ipython-input-41-e06f9ba74fae> in <module>()   1 f=open('states','r+') ----> 2 y=json.load(f)       3 f.close()       4 print y  c:\users\yael\downloads\winpython-64bit-2.7.10.2\python-2.7.10.amd64\lib\json\__init__.pyc in load(fp, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)     288         parse_float=parse_float, parse_int=parse_int,     289         parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, --> 290         **kw)     291      292   c:\users\yael\downloads\winpython-64bit-2.7.10.2\python-2.7.10.amd64\lib\json\__init__.pyc in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)     336             parse_int none , parse_float none ,     337             parse_constant none , object_pairs_hook none , not kw): --> 338         return _default_decoder.decode(s)     339     if cls none:     340         cls = jsondecoder  c:\users\yael\downloads\winpython-64bit-2.7.10.2\python-2.7.10.amd64\lib\json\decoder.pyc in decode(self, s, _w)     367         end = _w(s, end).end()     368         if end != len(s): --> 369             raise valueerror(errmsg("extra data", s, end, len(s)))     370         return obj     371   valueerror: data: line 1 column 13 - line 1 column 25 (char 12 - 24) 

why happening? tried changing x int , float same error occurs. thank ^^.

the error dump json twice. when want load again, not formed. try dump once , retry. or verify json correct in file saved.


Comments