zertap zertap - 2 years ago 141
Python Question

Reading utf-8 escape sequences from a file

I have an utf-8 encoded file that contains multiple lines like

\x02I don't like \x0307bananas\x03.\x02
Hey, how are you doing?
You called?

How do I read the lines of that file to a list, decoding all the escape sequences?
I tried the code below:

with codecs.open(file, 'r', encoding='utf-8') as q:
quotes = q.readlines()


But it prints the line without decoding escape characters.

\x02I don't like \x0307bananas\x03\x02

(Note: escape characters are IRC color codes,
being character for bolded text, and
prefix for color codes. Also, this code is from within my IRC bot, with the MSG function replaced by

Answer Source

According to this answer, changing the following should have the expected result.

In Python 3:

codecs.open(file, 'r', encoding='utf-8') to

codecs.open(file, 'r', encoding='unicode_escape')

In Python 2:

codecs.open(file, 'r', encoding='string_escape')

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download