用python编码元组列表? [英] encoding a list of tuples with python?

查看:428
本文介绍了用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 above u"\xe1" is really just <utf8 bytestring>"\xc3\xa1" which in turn is really all just á

这篇关于用python编码元组列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆