Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 从元组中提取元素以在python中编码_List_Tuples_Python Unicode_Unicode String - Fatal编程技术网

List 从元组中提取元素以在python中编码

List 从元组中提取元素以在python中编码,list,tuples,python-unicode,unicode-string,List,Tuples,Python Unicode,Unicode String,我有一个元组列表。有unicode问题。 我一直在努力把它编码成等价的字符,但我没有成功 以下是我的代码示例: import spaghetti as sgt import codecs f = codecs.open('output-data-pos', encoding='utf-8') raw = f.read() reviews = [raw.split()] output_tagged = (sgt.pos_tag_sents(reviews)) 下面是一个输出示例 [[(u'ce

我有一个元组列表。有unicode问题。 我一直在努力把它编码成等价的字符,但我没有成功

以下是我的代码示例:

import spaghetti as sgt
import codecs
f = codecs.open('output-data-pos', encoding='utf-8')
raw = f.read()
reviews = [raw.split()]
output_tagged = (sgt.pos_tag_sents(reviews))
下面是一个输出示例

 [[(u'cerramos', None), (u'igual', u'aq0cs0'), (u'arrancado', None), (u'estanter\xeda', None), (u'\xe9xito', u'ncms000'), (u'an\xe9cdotas', u'ncfp000')]]
我的总体目标是从元组中提取每个值,并将其编码为utf-8,以获得最终结果,如

 cerramos   None
 igual  aq0cs0
 arrancado  None
 estantería None
 éxito  ncms000
 anécdotas  ncfp000
到目前为止,我尝试过的一些策略来自简单的策略:

我尝试输出列表并直接对其进行编码

d = codecs.open('output-data-tagged', 'w', encoding='utf-8')
d.write(output_tagged)
还是这种方法

f = open('output-data-tagged', 'w')
for output in output_tagged:
    output.encode('utf-8')
    f.write(output)
f.close
我首先尝试映射列表,然后对其进行编码:

list_of_lists = map(list, output_tagged)
print list_of_lists
我尝试使用函数对数据进行编码

def reprunicode(u):
     return reprunicode(u).decode('raw_unicode_escape')

print u'[%s]' % u', '.join([u'(%s,)' % reprunicode(ti[0]) for ti in output_tagged])
这个也是:

def utf8data(list): 
     return [item.decode('utf8') for item in list]

print utf8data(output_tagged)

考虑到我的多次尝试,如何从列表中的元组中提取元素以获得我想要的最终编码结果?

您是否使用Python 3?@AhmadYoosofan否,python2.7很抱歉我的问题,因为您上一次代码中的打印指令显示了版本,我不记得答案了,因为我已经多年没有使用Python 2了,而我确实遇到了这些问题。如果您不需要只在Python 2上运行的特殊包,那么最好使用Python 3。