用python编码元组列表?

new*_*hon 1 python io encoding character-encoding python-2.7

我正在从一个目录中读取一个utf-8文本文件,然后我将readed文本插入列表中,我得到了一些这样的元组:

l = [('mucho','fácil'),...,('yo','hola')]
Run Code Online (Sandbox Code Playgroud)

当我在控制台上打印时,我有以下内容:

print l

('mucho','f\xc3\xa1cil'),...,('yo','hola')
Run Code Online (Sandbox Code Playgroud)

所以我尝试了以下方法:

fixing_l = [x.encode('utf-8') for x in l]
Run Code Online (Sandbox Code Playgroud)

当我尝试打印它时,我得到以下异常:

AttributeError: 'tuple' object has no attribute 'encode' 
Run Code Online (Sandbox Code Playgroud)

我如何编码和修复字符串并得到这样的东西?:

('mucho','fácil'),...,('yo','hola')
Run Code Online (Sandbox Code Playgroud)

Jor*_*ley 5

我认为你的意思是解码

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")
Run Code Online (Sandbox Code Playgroud)

如果你打印它,你应该看到正确的字符串.

由于你最初有一个正常的字节字符串,你需要decode它返回一个对象的unicode表示...在上面的情况下,u"\xe1"实际上只是<utf8 bytestring>"\xc3\xa1"反过来真的只是á