给定一个unicode对象:
u'[obj1,obj2,ob3]'
如何将其转换为正常的对象列表?
答案 0 :(得分:7)
import ast
s = u'[obj1,obj2,ob3]'
n = ast.literal_eval(s)
n
[obj1, obj2, ob3]
答案 1 :(得分:4)
>>> s = u'[obj1,obj2,ob3]'
>>> n = [e.encode('utf-8') for e in s.strip('[]').split(',')]
>>> n
['obj1', 'obj2', 'ob3']
答案 2 :(得分:0)
您发布的是unicode字符串。
要编码它,例如因为UTF-8使用yourutf8str = yourunicodestr.encode('utf-8')
答案 3 :(得分:0)
当unicode数据未显示unicode时...
使用openpyxl从excel表导出数据时,我的unicode是不可见的。使用print repr(s)
查看
>>>print(data)
>>>print(type(data))
["Independent", "Primary/Secondary Combined", "Coed", "Anglican", "Boarding"]
<type 'unicode>
>>>print repr(data)
u'["Independent", "Primary/Secondary Combined", "Coed", "Anglican", "Boarding"]'
修复:
>>>import ast
>>>data = ast.literal_eval(entry)
>>>print(data)
>>>print(type(data))
["Independent", "Primary/Secondary Combined", "Coed", "Anglican", "Boarding"]
<type 'list'>