PeterMmm
PeterMmm

Reputation: 24630

Decode file input

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.

Upvotes: 0

Views: 854

Answers (1)

dileep nandanam
dileep nandanam

Reputation: 2885

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')

Upvotes: 2

Related Questions