查找文件中除了注释以外的中文

在一些需要国际化的项目中,需要找出曾经代码中的中文替换为其他文字,我这里提供一种比较粗糙的查找小工具check_zh.py。有能改进的地方请大家指出。

环境是linux,python2.6

代码如下:

  1 #/usr/bin/env python
  2 #coding:utf-8
  3 
  4 import os,sys
  5 import re
  6 import codecs
  7 reload(sys)
  8 sys.setdefaultencoding('utf8')
  9 
 10 
 11 zhPattern = re.compile(u'[u4e00-u9fa5]+')  #用于匹配有中文的行
 12 file_types = ['.pyc','.jpg','.png','.xls,','.svn-base']   #默认过滤掉的二进制文件的扩展名
 13 choice_tpye_list = []  #输入要查找的文件格式
 14 save_path = ''   #结果保存位置
 15 
 16 
 17 
 18 def start():
 19     '''
 20     argv[1]:要查询的文件或目录的绝对路径
 21     argv[2]:要查询的文件的格式,输入*或者不输入将按默认设置查
 22     argv[3]:查询结果的保存路径,绝对路径 
 23     :return: 
 24     '''
 25     global choice_tpye_list,save_path
 26     try:
 27         paths = sys.argv[1]
 28         print 'paths',paths
 29     except Exception:
 30         paths = ''
 31     try:
 32         choices = sys.argv[2]
 33         print 'choices', choices
 34         if choices == '*':
 35             choices = ''
 36     except Exception:
 37         choices = ''
 38     try:
 39         save_path = sys.argv[3]
 40         print 'save_path', save_path
 41     except Exception:
 42         save_path = os.path.join(os.getcwd(),'check_zh.txt')
 43     choice_types = choices.split(',')
 44     if len(choice_types) == 1 and '' in choice_types:  #choice_types可能是[''],其布尔值为True
 45         pass
 46     else:
 47         choice_tpye_list = choice_types
 48     path_list = paths.split(',')
 49     for path in path_list:
 50         checkDIR(path)
 51 
 52 
 53 def checkDIR(path):
 54     '''检查是不是文件,是文件处理,不是则向下查找文件'''
 55     if os.path.isfile(path):
 56         a,b = os.path.splitext(path) # 去除扩展名
 57         if choice_tpye_list:
 58             if b in choice_tpye_list:   #检查当前文件扩展名是不是指定查询的扩展名文件
 59                 checkZh(path)
 60         else:
 61             if b not in file_types:
 62                 checkZh(path)
 63 
 64     elif os.path.isdir(path):
 65         file_list = os.listdir(path)
 66         path_list = map(lambda x: os.path.join(path, x), file_list)  # 转为绝对路径
 67         for item in path_list:
 68             checkDIR(item)
 69     else:
 70         print u'---输入错误---'
 71 
 72 def checkZh(file):
 73     '''查找文件中的中文位置'''
 74     num = 1
 75     all_lis = []
 76     lis = []
 77     with open(file, 'r') as f:
 78         line = f.readline()
 79         while line:
 80             try:
 81                 line = line.decode('utf-8')
 82             except Exception,e:
 83                 print e,'----',file,'---',num,'---',line,u'---文件可能是个二进制文件'
 84 
 85             content_lis = line.split('#')
 86             match = zhPattern.search(content_lis[0])
 87             if match:
 88                 lis = [file, num, line]
 89                 all_lis.append(lis)
 90             line = f.readline()
 91             num += 1
 92 
 93     with codecs.open(save_path, 'a','utf-8') as f:
 94         f.write('文件%s的查找结果:
'%file)
 95         if all_lis:
 96             for itme in all_lis:
 97                 f.write('    %s   第%s行   %s
' % (itme[0],itme[1], itme[2]))
 98         else:
 99             f.write('    无相关结果
')
100 
101 
102 
103 
104 
105 if __name__ == '__main__':
106     start()
107     print u'***查找结果将在:%s显示***'%save_path
原文地址:https://www.cnblogs.com/hujq1029/p/6914083.html