我有一个保存在文本中的[令牌,标签]列表,当我在python中打开列表时,它不会显示为列表。列表看起来像这样
[(u'porta', 'NN'); (u'scuola', 'NN'); (u'ragazzo', 'NN')]
在python中我以这种方式打开它
L = codecs.open('/directoryoflist/list', 'r', 'utf-8').read()
当我做
时for i L:
print i:
#it does not give me the item in the list
有人可以帮忙吗?
答案 0 :(得分:0)
您可以使用ast.literal_eval
:
>>> import re
>>> import ast
>>> a="[(u'porta', 'NN'); (u'scuola', 'NN'); (u'ragazzo', 'NN')]"
>>> my_list = ast.literal_eval(re.sub(';',',',a))
>>> my_list
[(u'porta', 'NN'), (u'scuola', 'NN'), (u'ragazzo', 'NN')]
>>> type(my_list)
<type 'list'>