Reading Utf-8 Escape Sequences From A File
Solution 1:
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')
Solution 2:
If you want to output text to console with the same formatting, then the point is, that UNIX (or what OS do you use?) uses ANSI escape sequences different from those in IRC, so you have to translate IRC format to UNIX format. these are the links to start: https://stackoverflow.com/a/287944/2660503Color text in terminal applications in UNIX
If you want to print text without formating, just clean it, by using regexp.
Solution 3:
The solution, as some people suggested is using codecs.open(file, 'r', encoding='unicode_escape')
, which will look like the following once implemented:
with codecs.open(file, 'r', encoding='unicode_escape') as q:
quotes = q.readlines()
print(str(random.choice(quotes)))
If you use regular utf-8 decoding, the result for \x02I don't like \x0307bananas\x03.\x02
will actually be "\\x02I don't like \\x0307bananas\\x03.\\x02\n"
because readlines()
method will escape the characters for you
Post a Comment for "Reading Utf-8 Escape Sequences From A File"