2

I have a list of objects, that has been stringified:

u'[<object: objstuff1, objstuff2>, <object: objstuff1, objstuff2>]'

I want to convert this back into a list:

[<object: objstuff1, objstuff2>, <object: objstuff1, objstuff2>]

I've tried using ast.literal_eval(), but unfortunately, it doesn't seem to work if the elements are objects, and I get a SyntaxError.

Is there any way I can reconvert my string representation of the list of objects back into a list?

1 Answer 1

2

You need to have a look at the pickle module to do this.

Basically, dump your objects using pickle.dumps, and load them back using pickle.loads.

ast.literal_eval doesn't work obviously, because there is a lot of information related to the objects (like attributes, and values) which is simply not captured in that string. Also note that you will be able to resurrect only the pickled data, if all you have are those string representations right now, you won't be able to create the objects back from them because of the information loss.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.