我有一个这样的列表
d=[{u'Length': u'2.96m', u'Width': u'1.44m', u'height': u'0.38m'},
{u'Length': u'3.8m', u'Width': u'0.65m', u'height': u'9.3m'},
{u'Length': u'0.62m', u'Width': u'2.9m', u'height': u'3.5m'}]
我希望有一种简单的方法来从这个列表中删除Unicode 'u',并创建一个新的列表。这里的"简单方法"是指不导入外部模块或将其保存在外部文件中即可去除Unicode。
这是我尝试过的五种方法:
def to_utf8(d):
if type(d) is dict:
result = {}
for key, value in d.items():
result[to_utf8(key)] = to_utf8(value)
elif type(d) is unicode:
return d.encode('utf8')
else:
return d
#these three returns AttributeError: 'list' object has no attribute 'encode'
d.encode('utf-8')
d.encode('ascii')
d.encode("ascii","replace")
#output the same
to_utf8(d)
print str(d)
前三个返回值
属性错误:'list'对象没有'encode'属性
最后两个打印出相同的结果。如何去除Unicode中的'u'?