Win7下 Python中文正则的奇异表现

首先贴Win32下的直接Python shell下的测试结果:

Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import re
>>> print re.match(ur'[\u4e00-\u9fa5]+',u"中国")
None
>>> 
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> str1=u"中国"
>>> str1
u'\xd6\xd0\xb9\xfa'

接着来先cmd,再python命令进入python交互环境下的结果:

Microsoft Windows [版本 6.1.7601]
版权所有 (c) 2009 Microsoft Corporation。保留所有权利。

C:\Users\user>python
Python 2.7.6 (default, Nov 10 2013, 19:24:18) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> print re.match(ur'[\u4e00-\u9fa5]+',u"中国")
<_sre.SRE_Match object at 0x00522C28>
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> str1=u"中国"
>>> str1
u'\u4e2d\u56fd'

生产应用中的问题好像是因为codeing设置的添加而解决,和上面这个问题有一定关系,以上形式是交互环境下测试所得的结果。

参考资料:http://www.limijd.me/2013/02/python%E9%87%8Cunicode%E5%92%8Cutf-8%E7%9A%84%E5%8C%BA%E5%88%AB/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值