使用Python提取中文字符
#功能:国际化测试,用于提取应用设计包中的中文字符,并输出report
#解压---筛选---整理路径---提取中文---输出报告
#################################################################
#author: 陈月白
#_blogs: http://www.cnblogs.com/chenyuebai/
################################################################# #功能:国际化测试,用于提取应用设计包中中文字符,并输出report
#yuebai 20160328
#解压---筛选---整理路径---提取中文---输出报告 #-*- coding: utf-8 -*- import os
import shutil
import sys
import re
import zipfile
import glob workPath = "C:\\users\\yuebai\\Desktop\\国际化测试包"
reportPath = "C:\\users\\yuebai\\Desktop\\国际化输出报告" #定义日志函数
def logInfo(info):
log = open("%s\\run.log"%workPath,"a")
log.write("[Info] %s\n"%info)
log.close() #定义解压文件函数
def extractZip(fileName,extraPath):
f = zipfile.ZipFile(fileName,"r")
f.extractall(extraPath)
f.close()
logInfo("%s文件解压完成"%fileName) #功能函数,找出中文字符
def getChinese(fileFullPath):
isChinese = re.compile("([\u4e00-\u9fa5]+)+?")
f = open(fileFullPath,"r",encoding="UTF-8") #打开待提取文件
f_in = open("%s\\chineseTxt.txt"%reportPath,"a") #打开输出文件 for line in f.readlines():
getStr = isChinese.findall(str(line)) #逐行判断提取中文
if not getStr == []:
f_in.write("发现中文字符(╯' - ')╯︵ ┻━┻ ,文件路径为%s\n"%fileFullPath)
f_in.write("%s\n"%line) f.close()
f_in.close() logInfo("查找完成,输出报告路径:%s"%reportPath) #功能函数,列出路径下所有文件
def listAny(workPath):
if not os.path.exists(workPath):
print("Error,no such dictionary%s,plz check"%workPath)
zipList = os.listdir(workPath)
return zipList #删除非zip类型的包
notZipList = glob.glob("%s\\*[!p]"%workPath)
logInfo("notZipList =%s,prepare to delete"%notZipList)
for i in notZipList:
os.remove(i)
logInfo("删除非zip包完成") #获取zip包列表
zipList = listAny(workPath) #取zip包解压
#print("开始提取")
for zipPackage in zipList:
zipName = os.path.split(zipPackage)[0] #切割获取文件名
extraPath = os.path.join(workPath,zipName) #在当前文件夹下创建和zip包同名文件夹,用以做解压目标路径
os.makedirs(extraPath)
logInfo("构造解压路径完成,extraPath =%s"%extraPath) extraFilePath = os.path.join(workPath,zipPackage) #待解压文件绝对路径 #开始解压zip包,完成后删除源zip文件
extractZip(extraFilePath, extraPath)
os.remove(extraFilePath) #将扩展目录下流程文件汇总至\\plan下
if os.path.exists("%s\\Plans\\Extend"%extraPath):
tmpExtendPath = ("%s\\Plans\\Extend"%extraPath)
tmpPlanPath = ("%s\\Plans"%extraPath) for t in os.listdir(tmpExtendPath):
t_FullPath = ("%s\\%s\\"%(tmpExtendPath,t))
#print("t_FullPath =",t_FullPath)
if os.path.isfile(t_FullPath):
shutil.move(t_FullPath,tmpPlanPath) #调用getChinese,提取中文字符
for y in os.listdir(tmpPlanPath):
y_fullPath = os.path.join(tmpPlanPath,y)
#print(y_fullPath) logInfo("开始检查文件%s,检查结果路径:%s"%(y_fullPath,reportPath))
getChinese(y_fullPath) #剔除注释
f_in = open("%s\\chineseTxt.txt"%reportPath,"r") #全部中文文件
f_comment = open("%s\\comment.txt"%reportPath,"a") #打开待写入注释文件
f_result = open("%s\\result.txt"%reportPath,"a") #结果文件 for line in f_in.readlines():
if re.findall("^//.*",line):
f_comment.write("%s\n"%line)
else:
f_result.write("%s\n"%line) f_in.close()
f_comment.close()
f_result.close() print("提取完成,结果路径:%s"%reportPath)
使用Python提取中文字符的更多相关文章
- python处理中文字符
1.在py文件中使用中文字符 unicode.py文件内容如下所示: # -*- coding:utf-8 -*- str_ch = '我们women' uni_ch = u'我们women' pri ...
- [python]有中文字符程序异常的解决方案
一. 含有中文字符无法运行 在python3中用的是Unicode编码,Unicode号称万国码,可以向所有的编码进行兼容.不会出现这种问题. Python2中使用的是ASCII编码,会出现这种问题. ...
- python 匹配中文字符
参考: http://hi.baidu.com/nivrrex/blog/item/e6ccaf511d0926888d543071.html http://topic.csdn. ...
- python实现中文字符繁体和简体中文转换-乾颐堂
需求:把中文字符串进行繁体和简体中文的转换: 思路:引入简繁体处理库,有兴趣的同学可以研究一下内部实现,都是python写的 1.下载zh_wiki.py及langconv zh_wiki.py:ht ...
- Python解决中文字符的问题
from __future__ import unicode_literals print(type("test")) #<type 'unicode'> Chinat ...
- python 连接数据库-设置oracle ,mysql 中文字符问题
import cx_Oracle import MySQLdb def conn_oracle(): cnn = cx_Oracle.connect('用户名','密码','ip:端口号/数据库') ...
- python中文字符乱码(GB2312,GBK,GB18030相关的问题)
转自博主 crifan http://againinput4.blog.163.com/blog/static/1727994912011111011432810/ 在玩wordpress的一个博客搬 ...
- Python中文字符的理解:str()、repr()、print
Python中文字符的理解:str().repr().print 字数1384 阅读4 评论0 喜欢0 都说Python人不把文字编码这块从头到尾.从古至今全研究通透的话是完全玩不转的.我终于深刻的理 ...
- 中文字符 unicode转utf-8函数 python实现
unicode编码范围 00000000-0000007F的字符,用单个字节来表示: 00000080-000007FF的字符用两个字节表示 (中文的编码范围) 00000800-0000FFFF的字 ...
随机推荐
- HDU 4291 A Short problem(矩阵+循环节)
A Short problem Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Android 开发笔记___switch__开关
default switch <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ...
- iPad web APP 开发相关
1.移除 browser chrome,全屏启动 <meta name="apple-mobile-web-app-capable" content="yes&q ...
- React + Node 单页应用「一」前端搭建
项目地址 预览地址 原文地址 记录最近做的一个 demo,前端使用 React,用 React Router 实现前端路由,Koa 2 搭建 API Server, 最后通过 Nginx 做请求转发. ...
- Windows系统下python3中安装pyMysql
python2和python3是不兼容的,在py2中,链接数据库使用的是mysqldb,但在py3中是不能用的. 解决办法就是在py3中数据库使用的模块是pyMysql. 在dos窗口中安装第三方库会 ...
- 使用原生JavaScript的Canvas实现拖拽式图形绘制,支持画笔、线条、箭头、三角形、矩形、平行四边形、梯形以及多边形和圆形,不依赖任何库和插件,有演示demo
前言 需要用到图形绘制,没有找到完整的图形绘制实现,所以自己实现了一个 - - 一.实现的功能 1.基于oop思想构建,支持坐标点.线条(由坐标点组成,包含方向).多边形(由多个坐标点组成).圆形(包 ...
- Windows Forms DataGridView中合并单元格
Windows Forms DataGridView 没有提供合并单元格的功能,要实现合并单元格的功能就要在CellPainting事件中使用Graphics.DrawLine和 Graphics.D ...
- Docker Swarm 中最重要的概念- 每天5分钟玩转 Docker 容器技术(94)
从主机的层面来看,Docker Swarm 管理的是 Docker Host 集群.所以先来讨论一个重要的概念 - 集群化(Clustering). 服务器集群由一组网络上相互连接的服务器组成,它们一 ...
- JDBC数据源 使用JNDI连接池实现数据库的连接
0.引言 许多Web应用程序需要通过JDBC驱动程序访问数据库,以支持该应用程序所需的功能.Java EE平台规范要求Java EE应用程序服务器为此目的提供一个DataSource实现(即,用于JD ...
- linux操作系统基础篇(七)
Linux服务篇(二) 1.nfs服务的搭建 安装: yum install rpcbind nfs-utils -y 配置: NFS服务的配置文件为 /etc/exports,这个文件是NFS的主要 ...