本文实例讲述了Python正则表达式匹配中文用法。分享给大家供大家参考,具体如下:

#!/usr/bin/python
#-*- coding:cp936-*-#思路,将str转换成unicode,方可用正则表达式,前提是,要知道文件的编码,本例中是gbk
import cPickle as mypickle
import re
import sys
if (__name__=='__main__'):
  fid1=file('demo.txt','r');#demo.txt写入字符如:脚本之家
  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'

运行效果图如下:

PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:

JavaScript正则表达式在线测试工具:
http://tools.uoften.com/regex/javascript

正则表达式在线生成工具:
http://tools.uoften.com/regex/create_reg

更多关于Python相关内容可查看本站专题:《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

点赞(65)

评论列表共有 0 条评论

立即
投稿
返回
顶部