Is there a Python module to parse string notation in the source string?

Possible duplicate:
Handle escape sequences in a string in Python

If I get this line, for example, from a web form:

'\n test' 

The notation '\n' will not be interpreted as a line break. How do I parse this line so that it becomes a line break?

Of course, I can use replace , split , re , etc. to do it manually.

But perhaps there is a module for this, since I do not want me to be forced to process all \something notation manually.

I tried turning it into bytes and then using str as a constructor, but this does not work:

 >>> str(io.BytesIO(ur'\n'.encode('utf-8')).read()) '\\n' 
+7
source share
1 answer

Use .decode ('string_escape')

 >>> print "foo\\nbar\\n\\tbaz" foo\nbar\n\tbaz >>> print "foo\\nbar\\n\\tbaz".decode('string_escape') foo bar baz 

When I type in the code, the above should go away from \, so that the string contains 2 characters \ n

Edit: This is actually a duplicate of Escape Sequences in a String in Python

+13
source

All Articles