Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/335.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/7/python-2.7/5.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
用python编码元组列表?_Python_Python 2.7_Encoding_Io_Character Encoding - Fatal编程技术网

用python编码元组列表?

用python编码元组列表?,python,python-2.7,encoding,io,character-encoding,Python,Python 2.7,Encoding,Io,Character Encoding,我从一个目录中读取一个utf-8文本文件,然后将读取的文本插入一个列表中,我得到一些元组,如下所示: l = [('mucho','fácil'),...,('yo','hola')] 在控制台上打印时,我有以下内容: print l ('mucho','f\xc3\xa1cil'),...,('yo','hola') fixing_l = [x.encode('utf-8') for x in l] 因此,我尝试了以下方法: print l ('mucho','f\xc3\xa1ci

我从一个目录中读取一个utf-8文本文件,然后将读取的文本插入一个列表中,我得到一些元组,如下所示:

l = [('mucho','fácil'),...,('yo','hola')]
在控制台上打印时,我有以下内容:

print l

('mucho','f\xc3\xa1cil'),...,('yo','hola')
fixing_l = [x.encode('utf-8') for x in l]
因此,我尝试了以下方法:

print l

('mucho','f\xc3\xa1cil'),...,('yo','hola')
fixing_l = [x.encode('utf-8') for x in l]
当我尝试打印它时,会出现以下异常:

AttributeError: 'tuple' object has no attribute 'encode' 
如何对字符串进行编码和修复,并获得类似的结果

('mucho','fácil'),...,('yo','hola')

我想你是说解码

l = [('mucho','f\xc3\xa1cil'),...,('yo','hola')]
decoded = [[word.decode("utf8") for word in sets] for sets in l]


for words in decoded:
    print u" ".join(words)

print 'f\xc3\xa1cil'.decode("utf8")
如果您打印它,您应该会看到正确的字符串


由于您最初有一个正常的字节字符串,因此需要对其进行
解码
以返回对象的unicode表示形式。。。在上面的例子中,
u“\xe1”
实际上只是
“\xc3\xa1”
,而这反过来又是真正的

我想你的意思是解码

l = [('mucho','f\xc3\xa1cil'),...,('yo','hola')]
decoded = [[word.decode("utf8") for word in sets] for sets in l]


for words in decoded:
    print u" ".join(words)

print 'f\xc3\xa1cil'.decode("utf8")
如果您打印它,您应该会看到正确的字符串


由于您最初有一个正常的字节字符串,因此需要对其进行
解码
以返回对象的unicode表示形式。。。在上面的例子中,
u“\xe1”
实际上就是
“\xc3\xa1”
,而在python3中,它实际上就是
,您可以使用:

res = [tuple(map(lambda x: x.encode(encoding), tup)) for tup in list_tuples]
例如:

list_tuples = [('mucho','fácil'), ('\u2019', 't')]
res = [tuple(map(lambda x: x.encode('utf-8'), tup)) for tup in list_tuples]
结果:

[(b'mucho', b'f\xc3\xa1cil'), (b'\xe2\x80\x99', b't')]

在python3中,您可以使用:

res = [tuple(map(lambda x: x.encode(encoding), tup)) for tup in list_tuples]
例如:

list_tuples = [('mucho','fácil'), ('\u2019', 't')]
res = [tuple(map(lambda x: x.encode('utf-8'), tup)) for tup in list_tuples]
结果:

[(b'mucho', b'f\xc3\xa1cil'), (b'\xe2\x80\x99', b't')]

如果您想调用
encode
,它将不起作用;元组中的内容是
好的,我会将其更改为decode当您打印容器时,您总是不可避免地会看到容器项的
repr
。因此,无法创建一个元组列表,在打印时显示项“
str
,而不是它们的
repr
。如果需要的话,您需要一个自定义的容器类!在Python3中,字符串的
repr
恰好是您在这里想要的;在Python2.7中,当字符串包含非ASCII字符时,情况并非如此。这就是为什么你需要一些定制技巧。。。如果有(因为在Python2.7中,
\uuuuu repr\uuuu
只能返回ASCII字符);元组中的内容是
好的,我会将其更改为decode当您打印容器时,您总是不可避免地会看到容器项的
repr
。因此,无法创建一个元组列表,在打印时显示项“
str
,而不是它们的
repr
。如果需要的话,您需要一个自定义的容器类!在Python3中,字符串的
repr
恰好是您在这里想要的;在Python2.7中,当字符串包含非ASCII字符时,情况并非如此。这就是为什么你需要一些定制技巧。。。如果有的话(因为在Python 2.7中,
\uuuuuu repr\uuuuu
必须只返回ASCII字符)。现在我使用了
f\xe1cil
而不是
fácil
。我应该尝试其他编码吗?。我在OSX中,我用终端查看文件的编码,它显示为utf8。@newWithPython您的意思是在您的txt文件中有另一个单词
f\xe1cl
?如果是这样,我猜您的文件包含多个编码的文本否,首先是:f\xc3\xa1cil,然后是这个解决方案:f\xe1cil.no,因为它是
u“f\xe1cil”
,它只是字符串的unicode表示形式。unicode是python用来表示非ascii字符的。。。。关于粗编码,有几种编码,但这些都只是表示现在我用了
f\xe1cil
而不是
fácil
。我应该尝试其他编码吗?。我在OSX中,我用终端查看文件的编码,它显示为utf8。@newWithPython您的意思是在您的txt文件中有另一个单词
f\xe1cl
?如果是这样,我猜您的文件包含多个编码的文本否,首先是:f\xc3\xa1cil,然后是这个解决方案:f\xe1cil.no,因为它是
u“f\xe1cil”
,它只是字符串的unicode表示形式。unicode是python用来表示非ascii字符的。。。。当然,有几种编码,但这些都只是表示