View utf-8 tuple element as string in python

1.5k Views Asked by At

I have a list with Unicode utf-8 tuples like:

((u'\u0d2a\u0d31\u0d1e\u0d4d\u0d1e\u0d41', u'\u0d15\u0d47\u0d3e\u0d23\u0d4d\u200d\u0d17\u0d4d\u0d30\u0d38\u0d4d'), 7.5860818562067314)

I want to convert the utf-8 code as string. I have tried decode. But getting error. Can anyone please help me out?

Thanks in advance!

2

There are 2 best solutions below

0
On

Those are not UTF-8.

print data[0][0], data[0][1]
1
On

You should use .encode('utf-8') method instead of .decode(), because strings are represented in unicode type, and we want to get byte strings.

Here is great how-to about string encoding in python 2.7, must read it: http://docs.python.org/2/howto/unicode.html

For example, data[0][0].encode('utf-8') produces normal result.