Abrial
Abrial

Reputation: 421

Unknown output unicode in python 2

I tried to implement this code from NLP UPC research group to retrieve synonyms for some entered words. when I ran the testing method

def test():
    "tests some functions"
    a=wn.get_words(True)
    print  'length of a: ', len(a)
    print 'a[0]: ', a[0].tostring().decode('utf-8')

the output is unknowing encoding

length of a:  16043
a[0]:  �����

in the same code the Unicode is already declared as

def _encode(data):
    return data.encode('utf8')

and the platform that I used (net beans 7.2.1)is configured to support utf-8 encoding

how to solve this problem?

Upvotes: 0

Views: 1071

Answers (2)

Abrial
Abrial

Reputation: 421

thank you for the answers. I used this command instead and it's worked with me

print 'a[0]: ', a[0].encode('utf-8')

Upvotes: 0

Martijn Pieters
Martijn Pieters

Reputation: 1122242

If you already configured your setup to handle UTF-8, you do not need to decode your string to a Unicode object. What will happen then is that Python uses the current encoding detected for sys.stdout.

Try not decoding:

print 'a[0]: ', a[0].tostring()

Upvotes: 1

Related Questions