用python编码元组列表? [英] encoding a list of tuples with python?
问题描述
我正在从一个目录中读取一个utf-8文本文件,然后我在列表中插入读取的文本,我获得了一些这样的元组:
l = [('mucho','fácil'),...,('yo','hola')]
pre>
当我在控制台上打印时,我有以下:
print l
('mucho','f \xc3\xa1cil'),...,('yo','hola')
所以我试过下面的:
fixing_l = [x.encode('utf-8')for x in l]
打印它我得到这个异常:
AttributeError:'tuple'object没有属性'encode'
我如何编码和修复字符串并得到这样的东西:
('mucho','fácil'),...,('yo','hola')
解决方案我认为你的意思是解码
l = [('mucho','f \xc3\xa1cil'),...,('yo','hola')]
decoded = [[word.decode(utf8 )for words in sets] for set in l]
对于解码中的单词:
print u.join(words)
如果您打印,请打印'f \xc3\a1cil'.decode(utf8)
你应该看到propper字符串
因为你内部有一个正常的字节字符串你需要
decode
它返回一个对象的unicode表示...在上面u\xe1
的情况下真的只是< utf8 bytestring>\xc3\\ \\ xa1
,其实也只是á
I'm reading from a directory a utf-8 text file, then i insert the readed text in a list and I'm obtaining some tuples like this:
l = [('mucho','fácil'),...,('yo','hola')]
When I print it on the console I have the following:
print l ('mucho','f\xc3\xa1cil'),...,('yo','hola')
So I tried the following:
fixing_l = [x.encode('utf-8') for x in l]
When I try to print it I get this exception:
AttributeError: 'tuple' object has no attribute 'encode'
How can I encode and fix the strings and get something like this?:
('mucho','fácil'),...,('yo','hola')
解决方案I think you mean decode
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")
if you print it you should see the propper string
since you intially have a normal byte string you need to
decode
it which returns a unicode representation of the object ... in the case aboveu"\xe1"
is really just<utf8 bytestring>"\xc3\xa1"
which in turn is really all justá
这篇关于用python编码元组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!