Possible Duplicate:
Process escape sequences in a string in Python
If I get this string, for example from a web form:
'\n test'
The '\n'
notation won't be interpreted as a line break. How an I parse this string so it becomes a line break?
Of course I can use replace
, split
, re
, etc, to do it manually.
But maybe there is a module for that, since I don't want to be forced to deal with all the \something
notations manually.
I tried to turn it into bytes then use str
as a construtor but that doesn't work:
>>> str(io.BytesIO(ur'\n'.encode('utf-8')).read())
'\\n'
Use .decode('string_escape')
>>> print "foo\\nbar\\n\\tbaz"
foo\nbar\n\tbaz
>>> print "foo\\nbar\\n\\tbaz".decode('string_escape')
foo
bar
baz
As I'm typing in code, the above have to escape the \ to make the string contain the 2 characters \n
Edit: actually this is a duplicate of Process escape sequences in a string in Python
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With