How to convert a list to unicode list
Hey guys,
I need to know which is the best way to convert a list to unicode
list.
Code :
nameStaff = ['Huey', 'Dewey', 'Cộng']
The 3rd element is already unicode character. So whenever I try to convert to Unicode list, got an error message below.
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xcc in position 3: ordinal not in range(128)
I search on StackOverFlow but still not find the solution.
Solution 1 :
answer = [unicode(item) for item in x]
Solution 2 :
map(unicode,lst)
The expect result :
nameStaff = [u'Huey', u'Dewey', u'Cộng']
Could you have any idead for this issuse ?
Have you tried
unicode(some_str, 'utf-8')
? It worked for me.Problem solved. Thank you for this solution, could you help me to explain the reason ?
Code :
Output :
But if I delete the first line coding:utf-8 this script has error on line 4.
First of all, you should wrap your code in a pair of ``` to syntax highlight it. If you show your code like that, I don’t know where a code block starts and ends. Furthermore, it looks like the first line starts with a #, which the markdown parser identifies as a header.
Edited:
# encoding: utf-8
at the first line of a python source file declares the encoding of it, which is default to ASCII. Then, without it, it wouldn’t understand what'Cộng'
means, because ASCII table doesn’t have'ộ'
.