0

I have a plain text file with this content:

Test: \u0410\u0412\u0422\u041e

I try to read that file in python and print the characters in unicode like this:

import codecs
f = codecs.open('b.txt', encoding='utf-8')
for line in f:
    print line

Output:

Test: \u0410\u0412\u0422\u041e

I was expeting this text:

Test: ABTO

"Test" following the cyrilic word for STOP.

PeterMmm
  • 24,152
  • 13
  • 73
  • 111
  • http://stackoverflow.com/questions/990169/how-do-convert-unicode-escape-sequences-to-unicode-characters-in-a-python-string – btstevens89 Aug 29 '12 at 14:36

1 Answers1

2

You have an ascii file with unicode escape sequence; of the form \u0410... we have to convert it to the form \\u0410.... so that we can apply decode function as follows.

f = open('b','r')
for line in f:
    line.replace('\u','\\u')
    print line.decode('unicode-escape')
dileep nandanam
  • 2,827
  • 18
  • 20