I’m saving my list in a text file converting it to a string.
when I read my list I get a string like this:
"['layer1', '1', '10', '10', 'pending', 'Pending', '1', '1', '1', [[1, 10]]]"
I was wondering if there is an easy way to convert it back to a list.
if it’s impossible, if there is a better way to save it and get it back?
Thanks!
(I’m Working with Python 2.6)
look at
ast.literal_eval. It should do what you want.This should be completely safe from malicious code (unlike
eval), but the object that it is able to read are must be simple objects (strings, ints, floats, None and bools or lists/tuples/dictionaries composed entirely of strings, ints, floats, None and bools).Of course, if having a human readable datafile isn’t necessary, you should take a look at
pickleorjsonas suggested by the other answers.EDIT
It has come to my attention that
jsonis very easy for a human to read (see comments below), so you should probably use that.