Python正則表達(dá)式匹配中文用法示例
本文實(shí)例講述了Python正則表達(dá)式匹配中文用法。分享給大家供大家參考,具體如下:
#!/usr/bin/python
#-*- coding:cp936-*-#思路,將str轉(zhuǎn)換成unicode,方可用正則表達(dá)式,前提是,要知道文件的編碼,本例中是gbk
import cPickle as mypickle
import re
import sys
if (__name__=='__main__'):
fid1=file('demo.txt','r');#demo.txt寫(xiě)入字符如:腳本之家
p=re.compile('(^\s+|\s+$)');
phanzigbk=re.compile('[\\x20-\\x7f]');
phanzi=re.compile(u'[\u4e00-\u9fa5]');#這里要加u,注意
commlines=fid1.readlines();
fid1.close();
dictfamilyname={};
dictfirstname={};
for line in commlines:
line=p.sub('',line);
print type(line);
print line;
uline=unicode(line,'gbk');
print type(uline);
candidates=phanzi.findall(uline);
print len(candidates);
if(len(candidates)==2):
print candidates[0];
familynamegbk=candidates[0].encode('gbk');#把unicode型的變量變成str型的變量
firstnamegbk=candidates[1].encode('gbk');
if(dictfamilyname.has_key(familynamegbk)):
dictfamilyname[familynamegbk]=dictfamilyname[familynamegbk]+1;
else:
dictfamilyname[familynamegbk]=1;
if(dictfirstname.has_key(firstnamegbk)):
dictfirstname[firstnamegbk]=dictfirstname[firstnamegbk]+1;
else:
dictfirstname[firstnamegbk]=1;
familynameitems=dictfamilyname.items();
print familynameitems;
firstnameitems=dictfirstname.items();
familynameitems.sort(key=lambda d:d[1],reverse=True);
firstnameitems.sort(key=lambda d :d[1],reverse=True);
fid=file('familyname.txt','w');
for m in familynameitems:
s=m[0]+'\t'+str(m[1]);
fid.write(s);
fid.write('\n');
fid.close();
fid=file('firstname.txt','w');
for m in firstnameitems:
s=m[0]+'\t'+str(m[1]);
fid.write(s);
fid.write('\n');
fid.close();
print 'finish'
運(yùn)行效果圖如下:

PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線(xiàn)測(cè)試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線(xiàn)生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專(zhuān)題:《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門(mén)與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python3使用pandas獲取股票數(shù)據(jù)的方法
今天小編就為大家分享一篇python3使用pandas獲取股票數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
Python隨機(jī)數(shù)種子(random seed)的使用
在科學(xué)技術(shù)和機(jī)器學(xué)習(xí)等其他算法相關(guān)任務(wù)中,我們經(jīng)常需要用到隨機(jī)數(shù),本文就詳細(xì)的介紹一下Python隨機(jī)數(shù)種子,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
使用python?AI快速比對(duì)兩張人臉圖像及遇到的坑
這篇文章主要介紹了如何使用python?AI快速比對(duì)兩張人臉圖像?實(shí)現(xiàn)過(guò)程比較簡(jiǎn)單,但是第三方python依賴(lài)的安裝過(guò)程較為曲折,下面是通過(guò)實(shí)踐對(duì)比總結(jié)出來(lái)的能夠支持的幾個(gè)版本,避免大家踩坑,需要的朋友可以參考下2023-02-02
Pytorch實(shí)現(xiàn)LSTM和GRU示例
今天小編就為大家分享一篇Pytorch實(shí)現(xiàn)LSTM和GRU示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01
requests.gPython?用requests.get獲取網(wǎng)頁(yè)內(nèi)容為空?’?’問(wèn)題
這篇文章主要介紹了requests.gPython?用requests.get獲取網(wǎng)頁(yè)內(nèi)容為空?’?’,溫行首先舉例說(shuō)明,具有一定得參考價(jià)值,需要的小伙伴可以參考一下2022-01-01
python利用百度云接口實(shí)現(xiàn)車(chē)牌識(shí)別的示例
這篇文章主要介紹了python利用百度云接口實(shí)現(xiàn)車(chē)牌識(shí)別的示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02

