views:

46

answers:

1

Hi,

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.

+1  A: 

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.

Ignacio Vazquez-Abrams
Thanks dude, working great. I've spent few hours trying to figure it out. I'm still quite a begginer to Python :)
j3nc3k