I have strings with control-characters in them, and I want to make them visible (for printing documentation of them, for instance).
For example, I have
dialect = csv.sniffer().sniff(csvfile.read(1024))
and I want to print the contents of
dialect.lineterminator
This obviously contains control-character(s). They don't become printable by sticking a "\" in front of them. I'd like to see \n \r or both, as appropriate.
As I'm using Python 3, similar questions have proposed using str.encode, such as
dialect.lineterminator.encode('unicode-escape')
but if I print this, I get
b'\\r\\n'
which is, in spite of its appearance, just two bytes. I want a unicode string such as
"\\r\\n"
which is a 4-character string. I'm not after the unicode encoding, but the escape sequence.
You can just convert the string to a raw string:
Note: this is for Python2 only, in 3 the equivalent is
str.encode('unicode_escape')
AFAIKHere's a similar question I recently asked.