Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'list' object has no attribute 'encode'

Tags:

I have a list of unicode objects and want to encode them to utf-8, but encoding doesn't seem to work.

the code is here :

>>> tmp = [u' test context'] >>> tmp.encode('utf-8') Traceback (most recent call last):   File "<stdin>", line 1, in <module> AttributeError: 'list' object has no attribute 'encode' >>> 

I can't understand why there is no attribute encode

like image 654
mt0s Avatar asked Feb 20 '11 00:02

mt0s


2 Answers

You need to do encode on tmp[0], not on tmp.

tmp is not a string. It contains a (Unicode) string.

Try running type(tmp) and print dir(tmp) to see it for yourself.

like image 100
Mikel Avatar answered Sep 29 '22 17:09

Mikel


You need to unicode each element of the list individually

[x.encode('utf-8') for x in tmp] 
like image 32
Prakhar Agarwal Avatar answered Sep 29 '22 19:09

Prakhar Agarwal