免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2482 | 回复: 0
打印 上一主题 下一主题

python正则表达式提取中文字符串 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-19 22:22 |只看该作者 |倒序浏览

                                                                                                要做国际化的版本,需要把中文字符串都提取出来翻译,写了这个python脚本。
               
               
               
               
               
                #!/usr/bin/python
# -*- coding: UTF-8 -*
'''
##
# @file match-chinese.py
# @brief 利用正则表达式提取中文字符串
# @author Jesse
# @version 1.0
# @date 2009-11-20
'''
import os,string
import re
directory = "."
output = "chinese.txt"
def match_chinese(s, f, i):
    global fd_output
    r = re.compile('\"[^\"]*[\x80-\xff]{3}[^\"]*\"')
    s_match = r.findall(s)
    for c in s_match:
        str = "%s ( %d ): %s\n" % (f, i, c)
        fd_output.write(str)
def istextfile(filename, blocksize = 512):
    return istext(open(filename).read(blocksize))
def istext(s):
    if "\0" in s:
        return 0
   
    if not s:
        return 1
    text_characters = "".join(map(chr, range(32, 127)) + list("\n\r\t\b"))
    _null_trans = string.maketrans("", "")
    t = s.translate(_null_trans, text_characters)
    if len(t)/len(s) > 0.30:
        return 0
    return 1
   
def read_file(f):
    if not istextfile(f):
        print "%s is NOT a text file" % (f)
        return
    #if not re.match(r".*\.[c|h]$", f):
    #    return
    i = 0
    fd = open(f,'r')
    buff = fd.readlines()
    for line in buff:
        i += 1
        match_chinese(line, f, i)
    fd.close()
def walk_dir(dir):
    try:
        if not os.path.exists(dir):
            print dir, ": No such file or directory."
            return
        if not os.path.isdir(dir):
            read_file(dir)
            return
        files = os.listdir(dir)
        for file in files:
            file_abs = dir + "/" + file
            if os.path.isdir(file_abs):
                print file_abs,"is ...DIR..."
                walk_dir(file_abs)
            else:
                read_file(file_abs)
    except:
        print "Exception occured"
def main():
    global fd_output
    fd_output = open(output, 'w')
    walk_dir(directory)
    fd_output.close()
if __name__ == "__main__":
    main()
参考资料:编码相关:http://bbs.chinaunix.net/viewthread.php?tid=907172http://bbs.chinaunix.net/viewthread.php?tid=975358http://initiative.yo2.cn/archives/624088http://blog.zol.com.cn/1356/article_1355079.htmlftp://ftp.astron.com/pub/file/file-5.03.tar.gzpython相关:http://man.lupaworld.com/content/manage/ringkee/basic.htmhttp://www.blogjava.net/Skynet/archive/2009/05/02/268628.htmlhttp://iregex.org/blog/regex-to-match-chinese.htmlhttp://hi.baidu.com/sinomazing/blog/item/cba18400f4473519738b6508.htmlhttp://blog.donews.com/limodou/archive/2004/08/30/83538.aspx
               
               
               
               
               
               
               
               
               
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/100881/showart_2098847.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP