python处理中文的小技巧(太精辟了)
2011-11-21 16:43
183 查看
相信第一次处理中文的朋友们可能都会对中文的encoding 和程序的报错很头疼。
如果你像我一样希望能够把事情尽快做好而不去深究,你可能会写一些异常处理的代码把 UnicodeEncodingError糊弄过去先,但当你开始怀疑有多少encoding出错的信息被你丢弃的时候,可能你会很惊奇。于是,你还是会想坐下来,(洗把脸)然后面对自己必须弄懂什么是utf-8,什么是 ‘gb2312′, 什么是 ‘gbk’ 和其中的猫腻。正如有时候猛撕小伤口上邦迪胶布的快感一样,有时候当你认真面对一些你平时一直回避的问题的时候(其实有时候需要的不是勇气), 你反而会觉得“不过如此”,并且能够一劳永逸的解决问题。
关于Python处理Unicode,我所能找到的最言简意赅的入门教程是:
Unicode In
简要罗列一下最重要最实用的点:
Solution
Decode early (尽早decode, 将文件中的内容转化成 unicode 再进行下一步处理)
Unicode everywhere (程序内部处理都用unicode)
Encode late (最后encode回所需的encoding, 例如把最终结果写进结果文件)
1. Decode early
Decode to <type ‘unicode’> ASAP
>>> def to_unicode_or_bust(
… obj, encoding=’utf-8′):
… if isinstance(obj, basestring):
… if not isinstance(obj, unicode):
… obj = unicode(obj, encoding)
… return obj
…
>>>
detects if object is a string and if so converts to unicode, if not already.
2. Unicode everywhere
>>> to_unicode_or_bust(ivan_uni)
u’Ivan Krsti\u0107′
>>> to_unicode_or_bust(ivan_utf8)
u’Ivan Krsti\u0107′
>>> to_unicode_or_bust(1234)
1234
3. Encode late
Encode to <type ‘str’> when you write to disk or print
>>> f = open(‘/tmp/ivan_out.txt’,'w’)
>>> f.write(ivan_uni.encode(‘utf-8′))
>>> f.close()
我以前一直觉得unicode相关的处理都是很 dirty 的工作,一般都会一边尝试,一边用异常处理去补丁,看完以上这个教程以后豁然开朗。
祝大家也能早日理清处理中文的时候的头绪,坦然直面“神秘”的unicode。http://coolshell.cn/articles/461.html#more-461
如果你像我一样希望能够把事情尽快做好而不去深究,你可能会写一些异常处理的代码把 UnicodeEncodingError糊弄过去先,但当你开始怀疑有多少encoding出错的信息被你丢弃的时候,可能你会很惊奇。于是,你还是会想坐下来,(洗把脸)然后面对自己必须弄懂什么是utf-8,什么是 ‘gb2312′, 什么是 ‘gbk’ 和其中的猫腻。正如有时候猛撕小伤口上邦迪胶布的快感一样,有时候当你认真面对一些你平时一直回避的问题的时候(其实有时候需要的不是勇气), 你反而会觉得“不过如此”,并且能够一劳永逸的解决问题。
关于Python处理Unicode,我所能找到的最言简意赅的入门教程是:
Unicode In
Python, Completely Demystified (揭秘Python Unicode)
简要罗列一下最重要最实用的点:Solution
Decode early (尽早decode, 将文件中的内容转化成 unicode 再进行下一步处理)
Unicode everywhere (程序内部处理都用unicode)
Encode late (最后encode回所需的encoding, 例如把最终结果写进结果文件)
1. Decode early
Decode to <type ‘unicode’> ASAP
>>> def to_unicode_or_bust(
… obj, encoding=’utf-8′):
… if isinstance(obj, basestring):
… if not isinstance(obj, unicode):
… obj = unicode(obj, encoding)
… return obj
…
>>>
detects if object is a string and if so converts to unicode, if not already.
2. Unicode everywhere
>>> to_unicode_or_bust(ivan_uni)
u’Ivan Krsti\u0107′
>>> to_unicode_or_bust(ivan_utf8)
u’Ivan Krsti\u0107′
>>> to_unicode_or_bust(1234)
1234
3. Encode late
Encode to <type ‘str’> when you write to disk or print
>>> f = open(‘/tmp/ivan_out.txt’,'w’)
>>> f.write(ivan_uni.encode(‘utf-8′))
>>> f.close()
我以前一直觉得unicode相关的处理都是很 dirty 的工作,一般都会一边尝试,一边用异常处理去补丁,看完以上这个教程以后豁然开朗。
祝大家也能早日理清处理中文的时候的头绪,坦然直面“神秘”的unicode。http://coolshell.cn/articles/461.html#more-461
相关文章推荐
- PYTHON处理中文的时候的一些小技巧
- python 字典中文key处理,读取,比较
- SnowNLP:•中文分词•词性标准•提取文本摘要,•提取文本关键词,•转换成拼音•繁体转简体的 处理中文文本的Python3 类库
- python用法:处理sqlite中的中文字符时遇到的问题
- 黄聪:解决python中文处理乱码,先要弄懂“字符”和“字节”的差别
- python中处理中文
- python 读取excel内容为中文的处理
- Python 编码转换与中文处理
- python字符串中的中文处理
- 用python + openpyxl处理excel(07+)文档 + 一些中文处理的技巧
- Python疑难杂症:SyntaxError: Non-ASCII character Python中文处理问题
- Python打开包含中文字符的url时返回错误的处理
- ubuntu下python处理mysql中文乱码问题
- Python 3.4中文处理
- Eclipse搭建Python开发环境+Python中文处理
- 详解 python 中文编码与处理
- python处理json数据中的中文
- Python:中文处理小结
- 使用Python脚本处理OC中的中文字符串
- Python Unicode与中文处理(文摘)