AttributeError:'list'对象没有属性'encode' [英] AttributeError: 'list' object has no attribute 'encode'

查看:5295
本文介绍了AttributeError:'list'对象没有属性'encode'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个unicode对象的列表,并希望将它们编码为utf-8,但编码似乎不起作用。



代码在这里:

 >>> tmp = [u'test context'] 
>>> tmp.encode('utf-8')
追溯(最近的最后一次呼叫):
文件< stdin>,第1行,< module>
AttributeError:'list'对象没有属性'encode'
>>>

我不明白为什么没有属性编码

解决方案

您需要在 tmp [0] encode >,不在 tmp



tmp 不是一个字符串



尝试运行类型(tmp) print dir(tmp)为自己查看。


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

解决方案

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.

这篇关于AttributeError:'list'对象没有属性'encode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆