j3nc3k
j3nc3k

Reputation: 65

Python repr function problem

I'm dealing with some text parsing in Python and for that purpose, it's good for me to apply repr() function on each string I'm gonna parse, but after the parsing, I need to convert some parsed substring back to the previous representation, because I want to print them and I'm not able to do this. I thought that str() function should get the string back to the human more readable form. But when I apply str function on that substring, nothing's changed. As I've said I need to print the string in human readable form, without printing escape sequences like \n, \t etc... But when I apply repr() to a string and then I want to convert it back, I don't know how, because str() function didn't do it. So my question is, how to convert the string back into human readable form?

Thanks for every reply.

Upvotes: 2

Views: 1575

Answers (1)

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799210

str() has no effect on objects that are already strings. You need to use eval() to undo a repr() where possible. Try using ast.literal_eval() instead though.

Upvotes: 3

Related Questions