jmnwong
jmnwong

Reputation: 1667

How to convert "\x09" back to tab

I'm reading in a file from Python that has the line:

#separator \x09

How do I convert the \x09 into a tab character (I'm going to later use this as a delimiter)?

Upvotes: 1

Views: 6572

Answers (1)

abarnert
abarnert

Reputation: 365953

>>> s = r'\x09'
>>> s.decode('unicode_escape')
u'\t'

Or, in Python 3.x (if you have a str rather than a bytes, because you can't decode a str):

>>> s = r'\x09'
>>> s.encode('unicode_escape').decode('unicode_escape')
>>> '\t'

See Python Specific Encodings in the codecs docs for details.

Upvotes: 4

Related Questions