2.9小节完成,并修改声明。
This commit is contained in:
20
README.rst
20
README.rst
@@ -3,11 +3,11 @@
|
|||||||
=========================================================
|
=========================================================
|
||||||
|
|
||||||
-----------------
|
-----------------
|
||||||
作者的话
|
译者的话
|
||||||
-----------------
|
-----------------
|
||||||
人生苦短,我爱Python!
|
人生苦短,我用Python!
|
||||||
|
|
||||||
笔者一直坚持使用Python3,因为它代表了Python的未来。虽然向后兼容是它的硬伤,但是这个局面迟早会改变的,
|
译者一直坚持使用Python3,因为它代表了Python的未来。虽然向后兼容是它的硬伤,但是这个局面迟早会改变的,
|
||||||
而且Python3的未来需要每个人的帮助和支持。
|
而且Python3的未来需要每个人的帮助和支持。
|
||||||
目前市面上的教程书籍,网上的手册大部分基本都是2.x系列的,专门基于3.x系列的书籍少的可怜。
|
目前市面上的教程书籍,网上的手册大部分基本都是2.x系列的,专门基于3.x系列的书籍少的可怜。
|
||||||
|
|
||||||
@@ -15,15 +15,8 @@
|
|||||||
为了Python3的普及,我也不自量力,想做点什么事情。于是乎,就有了翻译这本书的冲动了!
|
为了Python3的普及,我也不自量力,想做点什么事情。于是乎,就有了翻译这本书的冲动了!
|
||||||
这不是一项轻松的工作,却是一件值得做的工作:不仅方便了别人,而且对自己翻译能力也是一种锻炼和提升。
|
这不是一项轻松的工作,却是一件值得做的工作:不仅方便了别人,而且对自己翻译能力也是一种锻炼和提升。
|
||||||
|
|
||||||
后来想了下,一个人毕竟力量有限。如果能放到github上面来大家一起合作翻译,那效果就完全不一样了。
|
译者会坚持对自己每一句的翻译负责,力求高质量。但受能力限制,也难免有疏漏或者表意不当的地方。
|
||||||
希望热爱python的有志之士可以一起合作把这本书翻译完成。通过fork/pull request方式。体验一把合作的魅力。
|
如果译文中有什么错漏的地方请大家见谅,也欢迎大家随时指正: yidao620@gmail.com
|
||||||
翻译这本书的前提是对python基础要有相当深入的研究,请不要使用google翻译这样的工具,
|
|
||||||
需要能把原文作者的意思完全表达出来,不需要逐字翻译,但表意一定要准确!
|
|
||||||
|
|
||||||
目前已经将项目的文件都已经新建好了,如果想参与进来,只需要更新对应的文件内容,然后pull request给我就行,
|
|
||||||
其他一切事情都由我来负责,也希望想参与进来的可以认真对待自己每一句的翻译,力求高质量。
|
|
||||||
|
|
||||||
译文中有什么错漏的地方请大家见谅,也欢迎大家指正: yidao620@gmail.com
|
|
||||||
|
|
||||||
--------------------------------------------------------------
|
--------------------------------------------------------------
|
||||||
|
|
||||||
@@ -51,4 +44,5 @@
|
|||||||
.. _readthedocs: https://readthedocs.org/
|
.. _readthedocs: https://readthedocs.org/
|
||||||
.. _sphinx-rtd-theme: https://github.com/snide/sphinx_rtd_theme
|
.. _sphinx-rtd-theme: https://github.com/snide/sphinx_rtd_theme
|
||||||
.. _reStructuredText: http://docutils.sourceforge.net/docs/user/rst/quickref.html
|
.. _reStructuredText: http://docutils.sourceforge.net/docs/user/rst/quickref.html
|
||||||
.. _python3-cookbook: http://python3-cookbook.readthedocs.org/zh_CN/latest/
|
.. _python3-cookbook: http://python3-cookbook.readthedocs.org/zh_CN/latest/
|
||||||
|
|
||||||
|
|||||||
42
cookbook/c02/p09_normal_unicode.py
Normal file
42
cookbook/c02/p09_normal_unicode.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Topic: unicode字符串标准化表示
|
||||||
|
Desc :
|
||||||
|
"""
|
||||||
|
import unicodedata
|
||||||
|
|
||||||
|
|
||||||
|
def nor_unicode():
|
||||||
|
s1 = 'Spicy Jalape\u00f1o'
|
||||||
|
s2 = 'Spicy Jalapen\u0303o'
|
||||||
|
print(s1, s2)
|
||||||
|
print(s1 == s2)
|
||||||
|
print(len(s1), len(s2))
|
||||||
|
|
||||||
|
# 先将文本标准化表示
|
||||||
|
t1 = unicodedata.normalize('NFC', s1)
|
||||||
|
t2 = unicodedata.normalize('NFC', s2)
|
||||||
|
print(t1 == t2)
|
||||||
|
print(ascii(t1))
|
||||||
|
|
||||||
|
t3 = unicodedata.normalize('NFD', s1)
|
||||||
|
t4 = unicodedata.normalize('NFD', s2)
|
||||||
|
print(t3 == t4)
|
||||||
|
print(ascii(t3))
|
||||||
|
|
||||||
|
# 扩展的NFKC和NFKD
|
||||||
|
s = '\ufb01' # A single character
|
||||||
|
print(s, len(s))
|
||||||
|
print(unicodedata.normalize('NFD', s), len(unicodedata.normalize('NFD', s)))
|
||||||
|
print(unicodedata.normalize('NFKC', s), len(unicodedata.normalize('NFKC', s)))
|
||||||
|
print(unicodedata.normalize('NFKD', s), len(unicodedata.normalize('NFKD', s)))
|
||||||
|
|
||||||
|
# 消除变音符
|
||||||
|
t1 = unicodedata.normalize('NFD', s1)
|
||||||
|
print(''.join(c for c in t1 if not unicodedata.combining(c)))
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
nor_unicode()
|
||||||
|
|
||||||
@@ -3,10 +3,10 @@
|
|||||||
==============
|
==============
|
||||||
*关于译者*
|
*关于译者*
|
||||||
|
|
||||||
* 网名: 一刀
|
* 网名: 一刀
|
||||||
* QQ: 344063306
|
* QQ: 344063306
|
||||||
* Email: yidao620@gmail.com
|
* Email: yidao620@gmail.com
|
||||||
* 博客: http://yidao620c.iteye.com/
|
* 博客: http://yidao620c.iteye.com/
|
||||||
* GitHub: https://github.com/yidao620c
|
* GitHub: https://github.com/yidao620c
|
||||||
|
|
||||||
|
|
|
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
|
|
||||||
1. 夏丹 (767962863@qq.com)
|
1. 夏丹 (767962863@qq.com)
|
||||||
2. 张三 (zhangsan@gmail.com)
|
2. 张三 (zhangsan@gmail.com)
|
||||||
3. lisi (lisi@gmail.com)
|
3. 李四 (lisi@gmail.com)
|
||||||
|
|
||||||
|
|
|
|
||||||
|
|
|
|
||||||
|
|||||||
@@ -1,18 +1,101 @@
|
|||||||
============================
|
===========================
|
||||||
2.9 Unicode字符串转为标准正则式
|
2.9 将Unicode文本标准化
|
||||||
============================
|
===========================
|
||||||
|
|
||||||
----------
|
----------
|
||||||
问题
|
问题
|
||||||
----------
|
----------
|
||||||
todo...
|
你正在处理Unicode字符串,需要确保所有字符串在底层有相同的表示。
|
||||||
|
|
||||||
|
|
|
||||||
|
|
||||||
----------
|
----------
|
||||||
解决方案
|
解决方案
|
||||||
----------
|
----------
|
||||||
todo...
|
在Unicode中,某些字符能够用多个合法的编码表示。为了说明,考虑下面的这个例子:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
>>> s1 = 'Spicy Jalape\u00f1o'
|
||||||
|
>>> s2 = 'Spicy Jalapen\u0303o'
|
||||||
|
>>> s1
|
||||||
|
'Spicy Jalapeño'
|
||||||
|
>>> s2
|
||||||
|
'Spicy Jalapeño'
|
||||||
|
>>> s1 == s2
|
||||||
|
False
|
||||||
|
>>> len(s1)
|
||||||
|
14
|
||||||
|
>>> len(s2)
|
||||||
|
15
|
||||||
|
>>>
|
||||||
|
|
||||||
|
这里的文本"Spicy Jalapeño"使用了两种形式来表示。
|
||||||
|
第一种使用整体字符"ñ"(U+00F1),第二种使用拉丁字母"n"后面跟一个"~"的组合字符(U+0303)。
|
||||||
|
|
||||||
|
在需要比较字符串的程序中使用字符的多种表示会产生问题。
|
||||||
|
为了修正这个问题,你可以使用unicodedata模块先将文本标准化:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
>>> import unicodedata
|
||||||
|
>>> t1 = unicodedata.normalize('NFC', s1)
|
||||||
|
>>> t2 = unicodedata.normalize('NFC', s2)
|
||||||
|
>>> t1 == t2
|
||||||
|
True
|
||||||
|
>>> print(ascii(t1))
|
||||||
|
'Spicy Jalape\xf1o'
|
||||||
|
>>> t3 = unicodedata.normalize('NFD', s1)
|
||||||
|
>>> t4 = unicodedata.normalize('NFD', s2)
|
||||||
|
>>> t3 == t4
|
||||||
|
True
|
||||||
|
>>> print(ascii(t3))
|
||||||
|
'Spicy Jalapen\u0303o'
|
||||||
|
>>>
|
||||||
|
|
||||||
|
normalize()第一个参数指定字符串标准化的方式。
|
||||||
|
NFC表示字符应该是整体组成(比如可能的话就使用单一编码),而NFD表示字符应该分解为多个组合字符表示。
|
||||||
|
|
||||||
|
Python同样支持扩展的标准化形式NFKC和NFKD,它们在处理某些字符的时候增加了额外的兼容特性。比如:
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
>>> s = '\ufb01' # A single character
|
||||||
|
>>> s
|
||||||
|
'fi'
|
||||||
|
>>> unicodedata.normalize('NFD', s)
|
||||||
|
'fi'
|
||||||
|
# Notice how the combined letters are broken apart here
|
||||||
|
>>> unicodedata.normalize('NFKD', s)
|
||||||
|
'fi'
|
||||||
|
>>> unicodedata.normalize('NFKC', s)
|
||||||
|
'fi'
|
||||||
|
>>>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
||||||
----------
|
----------
|
||||||
讨论
|
讨论
|
||||||
----------
|
----------
|
||||||
todo...
|
标准化对于任何需要以一致的方式处理Unicode文本的程序都是非常重要的。
|
||||||
|
当处理来自用户输入的字符串而你很难去控制编码的时候尤其如此。
|
||||||
|
|
||||||
|
在清理和过滤文本的时候字符的标准化也是很重要的。
|
||||||
|
比如,假设你想清除掉一些文本上面的变音符的时候(可能是为了搜索和匹配):
|
||||||
|
|
||||||
|
.. code-block:: python
|
||||||
|
|
||||||
|
>>> t1 = unicodedata.normalize('NFD', s1)
|
||||||
|
>>> ''.join(c for c in t1 if not unicodedata.combining(c))
|
||||||
|
'Spicy Jalapeno'
|
||||||
|
>>>
|
||||||
|
|
||||||
|
最后一个例子展示了unicodedata模块的另一个重要方面,也就是测试字符类的工具函数。
|
||||||
|
combining()函数可以测试一个字符是否为组合字符。
|
||||||
|
在这个模块中还有其他函数用于查找字符类别,测试是否为数字字符等等。
|
||||||
|
|
||||||
|
Unicode显然是一个很大的主题。如果想更深入的了解关于标准化方面的信息,
|
||||||
|
请看考`Unicode官网中关于这部分的说明 <http://www.unicode.org/faq/normalization.html>`_
|
||||||
|
Ned Batchelder在`他的网站 <http://nedbatchelder.com/text/unipain.html>`_
|
||||||
|
上对Python的Unicode处理问题也有一个很好的介绍。
|
||||||
|
|
||||||
|
|||||||
@@ -4,15 +4,15 @@ Copyright
|
|||||||
|
|
||||||
| 书名: 《Python Cookbook》3rd Edition
|
| 书名: 《Python Cookbook》3rd Edition
|
||||||
| 作者: David Beazley, Brian K. Jones
|
| 作者: David Beazley, Brian K. Jones
|
||||||
| 版本: 第三版
|
| 译者: 熊能
|
||||||
|
| 版本: 第3版
|
||||||
| 出版日期: 2013年5月08日
|
| 出版日期: 2013年5月08日
|
||||||
| Copyright © 2013 David Beazley and Brian Jones. All rights reserved.
|
| Copyright © 2013 David Beazley and Brian Jones. All rights reserved.
|
||||||
|
|
||||||
|
|
|
|
||||||
|
|
||||||
更多发布信息请参考 http://oreilly.com/catalog/errata.csp?isbn=9781449340377
|
| 更多发布信息请参考 http://oreilly.com/catalog/errata.csp?isbn=9781449340377
|
||||||
|
|
||||||
|
|
|
|
||||||
|
|
|
|
||||||
|
|
|
|
||||||
|
|
|
||||||
@@ -2,6 +2,22 @@
|
|||||||
前言
|
前言
|
||||||
==================================
|
==================================
|
||||||
|
|
||||||
|
----------------------------------
|
||||||
|
译者的话
|
||||||
|
----------------------------------
|
||||||
|
人生苦短,我用Python!
|
||||||
|
|
||||||
|
译者一直坚持使用Python3,因为它代表了Python的未来。虽然向后兼容是它的硬伤,但是这个局面迟早会改变的,
|
||||||
|
而且Python3的未来需要每个人的帮助和支持。
|
||||||
|
目前市面上的教程书籍,网上的手册大部分基本都是2.x系列的,专门基于3.x系列的书籍少的可怜。
|
||||||
|
|
||||||
|
最近看到一本《Python Cookbook》3rd Edition,完全基于Python3,写的也很不错。
|
||||||
|
为了Python3的普及,我也不自量力,想做点什么事情。于是乎,就有了翻译这本书的冲动了!
|
||||||
|
这不是一项轻松的工作,却是一件值得做的工作:不仅方便了别人,而且对自己翻译能力也是一种锻炼和提升。
|
||||||
|
|
||||||
|
译者会坚持对自己每一句的翻译负责,力求高质量。但受能力限制,也难免有疏漏或者表意不当的地方。
|
||||||
|
如果译文中有什么错漏的地方请大家见谅,也欢迎大家随时指正: yidao620@gmail.com
|
||||||
|
|
||||||
----------------------------------
|
----------------------------------
|
||||||
作者的话
|
作者的话
|
||||||
----------------------------------
|
----------------------------------
|
||||||
|
|||||||
@@ -2,4 +2,43 @@
|
|||||||
Roadmap
|
Roadmap
|
||||||
===========
|
===========
|
||||||
|
|
||||||
todo...
|
2014/08/10 - 2014/08/31:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
| github项目搭建,readthedocs文档生成。
|
||||||
|
| 整个项目的框架完成
|
||||||
|
|
||||||
|
2014/09/01 - 2014/09/30:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
| 前4章翻译完成
|
||||||
|
|
||||||
|
|
||||||
|
2014/10/01 - 2014/10/31:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
| 前8章翻译完成
|
||||||
|
|
||||||
|
|
||||||
|
2014/11/01 - 2014/11/01:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
| 前12章翻译完成
|
||||||
|
|
||||||
|
2014/12/01 - 2014/12/31:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
| 所有15章翻译完成,包括附录
|
||||||
|
|
||||||
|
|
||||||
|
2015/01/01 - 2015/01/31:
|
||||||
|
|
||||||
|
::
|
||||||
|
|
||||||
|
| 对外公开发布完整版
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user