Frage

I'd need to go through all the element to do the string encoding conversion in the Python list. Try to use the list comprehension but it don't work.

Here is my code:

contain_list = [[u'\u6e2c\u8a66', 'b', 'c'], [u'\u5de5\u4f5c', 'b1', 'c1']]

[[x.encode('utf-8') for x in row] for row in contain_list]

But if I try:

for row in contain_list:
    for index in range(0,3):
        row[index] = row[index].encode('utf-8')

I can get the string converted in utf-8. how can I use the list comprehension to complete the work?

War es hilfreich?

Lösung

If you print each element of the contain_list, you can see that they got encoded correctly. If you see the content_list, you'll see only the utf-8 representation.

contain_list = [[u'\u6e2c\u8a66', 'b', 'c'], [u'\u5de5\u4f5c', 'b1', 'c1']]
contain_list = [[x.encode('utf-8') for x in row] for row in contain_list]

print contain_list

for each in contain_list:
    for e in each:
        print e

[['\xe6\xb8\xac\xe8\xa9\xa6', 'b', 'c'], ['\xe5\xb7\xa5\xe4\xbd\x9c', 'b1', 'c1']]

測試
b
c
工作
b1
c1

Andere Tipps

Actually you got what you want:

>>> contain_list = [[u'\u6e2c\u8a66', 'b', 'c'], [u'\u5de5\u4f5c', 'b1', 'c1']]
>>> r = [[x.encode('utf-8') for x in row] for row in contain_list]
>>> r[0][0]
'\xe6\xb8\xac\xe8\xa9\xa6'

which is UTF-8 representation of some Chinese I guess.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top