我用Python创建了一个词汇表,但是扩展Ascii代码有问题。
创建单词的循环是:(ascii数字128到164:e、a等)
#extented ascii codes
i = 128
while i <= 165 :
dictionnary[chr(i)] = 'extended ascii'
i = i + 1
但当我试图使用措辞时:
>>> dictionnary['è']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: '\xc3\xa8'
我在python脚本的头中有#--编码:utf-8--。 我试过编码、解码等,但结果总是不好。
为了了解发生了什么,我试过:
>>> ord('é')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: ord() expected a character, but string of length 2 found
以及
>>> ord(u'é')
233
我和ord(u'e')混淆了,因为'e'是扩展ascii表中的130号,而不是233号。
我知道扩展的ascii码包含“两个字符”,但我不知道如何用措辞来解决这个问题?
提前谢谢! :-)
使用
unichr
而不是chr
。函数chr
生成包含单个字节的字符串,而unichr
生成包含单个unicode字符的字符串。最后,也要使用unicode字符进行查找:d[u'é']
,因为d['é']
将查找的是e的utf-8编码。在代码中有三个元素:拉丁1编码的str、utf-8编码的str和unicode字符串。要在头脑中清楚地知道在任何时间点上都有哪些内容,就需要了解Python是如何工作的,并对Unicode和编码有相当的了解。
如果没有Joel Spolsky关于编码和Unicode的文章的链接,就没有完整的答案:The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
相关问题 更多 >
编程相关推荐