I wrote my data to a file using pprint.PrettyPrinter
and I am trying to read it using ast.literal_eval
.
This has been working for me for quite some time, and I am reasonably satisfied with the text representation produced.
However, today I got this error on deserialization:
File "/...mypath.../store.py", line 82, in <lambda>
reader=(lambda fd: ast.literal_eval(fd.read())),
File "/usr/lib64/python2.7/ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "/usr/lib64/python2.7/ast.py", line 60, in _convert
return list(map(_convert, node.elts))
File "/usr/lib64/python2.7/ast.py", line 63, in _convert
in zip(node.keys, node.values))
File "/usr/lib64/python2.7/ast.py", line 62, in <genexpr>
return dict((_convert(k), _convert(v)) for k, v
File "/usr/lib64/python2.7/ast.py", line 63, in _convert
in zip(node.keys, node.values))
File "/usr/lib64/python2.7/ast.py", line 62, in <genexpr>
return dict((_convert(k), _convert(v)) for k, v
File "/usr/lib64/python2.7/ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string
How do I fix this specific file?
The file in question is 17k lines/700kb. I loaded it into Emacs -- the parens are balanced. There are no non-ASCII characters in the file. I can "divide and conquer" (split the file in half and try to real each half) - but this is rather tedious. Is there anything better?
I modified ast.literal_eval:_convert
to print the offending node - it turned out to be <_ast.UnaryOp object at 0x110696510>
. Not very helpful.
How do I ensure that this does not happen in the future?
I hope JSON
is not the answer. ;-)
I am not using JSON
because
Quick and Dirty
Apply this patch:
Reload
ast
:Retry loading the offending file
Get
then line 21161, column 10 is where the error is.
Sophisticated
Wrap the code in
try/except
, catch the error and useinspect
/traceback
to access thenode
in question:prints