how to automatically escape control-characters in a Python string

19.9k views Asked by At

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.

1

There are 1 answers

1
Will On

You can just convert the string to a raw string:

>>> a = 'foo\nbar'
>>> print a
foo
bar
>>> print a.encode('string-escape')
foo\nbar

Note: this is for Python2 only, in 3 the equivalent is str.encode('unicode_escape') AFAIK

Here's a similar question I recently asked.