1. 程式人生 > >Win7下 Python中文正則的奇異表現

Win7下 Python中文正則的奇異表現

window rep logs ice 所有 windows 版權 命令 參考

首先貼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/

Win7下 Python中文正則的奇異表現