#!/usr/bin/python
#-*- coding:utf-8 –*-import os
import sys
import re
import shutil
import xlrd
import xlwt
import getopt
import math
from xlutils.copy import copy'''
脚本使用:设置strUiPorject ui项目名称,取值如下 "mstar"/"mstar_atv"/"formal"/"formal_grey"/"haier"/"videocon"/"bbk"/"atv_project"删除无用字串:             ./genstr -d特殊标记的字串优先排序:    ./genstr -p给已整理好优先级高的字串添加strMark..../genstr -a读取 优先级字串整理.h 中的字串进行比对添加标记(未整理)./genstr -c 脚本功能:1、根据strUiPorject设置的UI名称,迭代过滤UI目录的所有源文件和头文件,获取项目使用字串总数,并删除UIL多余字串2、对某一种语言做特殊标记strMark,标记的字串会放在翻译的最后面3、mstar优先级字串整理在mstar优先字串整理.h,已使用字串整理在mstar已使用字串整理.h中,formal优先级字串整理在formal优先字串整理.h
执行过程:1、设置UI项目名称2、根据UI名称,配置过滤路径和UIL删除路径(filterPath/strUilPath)3、再根据配置的路径执行过滤和删除动作注意:SourceCode中,有一些是 TV_IDS_String_ID+Offset方式获取新字串的,这些字串要手动加到脚本,以防误删如:TV_IDS_String_GMT_0 字串
'''#=======注意此处设置UI项目================================#
#=="mstar"/"formal"/"formal_grey"/"mstar_atv"/"haier"/"videocon"/"bbk"/"atv_project"=#
strUiPorject = "formal_grey"
#========================================================#

g_deleteMode = 0
g_priorityMode = 0
g_AddmarkMode = 0
g_CompareMode = 0setStr = set()
tupleStr = ()
strMark = "aaaa"#=======================以下不需要设置=======================#
if "bbk" == strUiPorject:filterPath = "aps/application/radisson/formal"strUilPath = "aps/application/radisson/formal/UI_Project/TV_UIProject/Languages"
elif "formal" == strUiPorject:filterPath = "aps/application/radisson/formal"strUilPath = "aps/application/radisson/formal/UI_Project/TV_UIProject_new/Languages"
elif "formal_grey" == strUiPorject:filterPath = "aps/application/radisson/formal"strUilPath = "aps/application/radisson/formal/UI_Project/TV_UIProject_grey/Languages"
elif "mstar_atv" == strUiPorject:filterPath = "aps/application/radisson/mstar"strUilPath = "aps/application/radisson/mstar/UI_Project/TV_UIProject_atv/Languages"
else:filterPath = "aps/application/radisson/%s" % strUiPorjectstrUilPath = "aps/application/radisson/%s/UI_Project/TV_UIProject/Languages" % strUiPorject
gamePath = "aps/game"listPath = [filterPath,gamePath]def filterUsefulString():listStrId = []for path in listPath:for  dirPath, dirNames, fileNames in os.walk(path):for  sourceFile in fileNames:filePath = dirPath+"/"+sourceFileif (re.search(".*\.c.*",sourceFile) or re.search(".*\.h.*",sourceFile)) \and sourceFile != "TV_strid.h":for line in open(filePath,"r"):if "TV_IDS_String" in line:if line.count("TV_IDS_String") > 2:print "\n\nthe number of string are more than 2 in a row \n\n "print sourceFileprint "\n"continueif re.search(".*TV_(IDS_String\w*).*TV_(IDS_String\w*).*",line):tupleStr=re.search(".*TV_(IDS_String\w*).*TV_(IDS_String\w*).*",line).groups()for i in range(len(tupleStr)):setStr.add(tupleStr[i])else:setStr.add(re.search(".*TV_(IDS_String\w*).*",line).group(1))elif "TV_IDS_Game_Menu_OSD_String" in line:setStr.add("IDS_Game_Menu_OSD_String")UsedStrfilename = strUiPorject + "已使用字串整理.h"print "\n\n程序中共使用 %d 个字串。\n保存在当前目录 %s-文件中\n\n" % (len(setStr),UsedStrfilename)for line in setStr:listStrId.append(line)listStrId.append("\n")open(UsedStrfilename,"w").writelines(listStrId)'''#读EXCEL到映射表
def excelSetting()setElStr = set()mapStr = {}listFirst = []setDiff = set()book = xlrd.open_workbook(r'Languages.xls')sheet = book.sheet_by_index(0)listFirst = sheet.row_values(0)for row in range(sheet.nrows):cellStr = str(sheet.cell(row,0).value)cellStr.rstrip()if cellStr in setStr:mapStr[cellStr] = sheet.row_values(row)#setElStr = set(mapStr.keys())#setDiff = setElStr - setStr#写EXCELwboot = xlwt.Workbook()sheet = wboot.add_sheet("Language")#操作第一行,抬头for col in range(len(listFirst)):sheet.write(0,col,listFirst[col])#其它行row = 1for (k,v) in mapStr.items():for col in range(len(v)):sheet.write(row,col,v[col])row = row + 1wboot.save(r'Language_.xls')'''
#处理UIL文件,对比setStr集合,删除无用字串
def deleteString():delCount = 0lanList = []for dirPath,dirNames,fileNames in os.walk(strUilPath):for sourceFile in fileNames:filePath = dirPath + "/" + sourceFilefor line in open(filePath,"r"):#==============================================##有些字串在code中是以偏移量的方式使用,不能删除if "IDS_String_GMT_" in line:lanList.append(line)continueelif re.search(".*IDS_String_\d{1,2}\".*",line) or ("IDS_String_LNB" in line):lanList.append(line)continue# ==============================================#if "<String ID=" in line:if re.search("\s*<String ID=\"(\w*)\".*",line).group(1) in setStr:lanList.append(line)else:delCount = delCount+1else:lanList.append(line)open(filePath,"w").writelines(lanList)print(sourceFile + "删除 %s" %delCount)lanList = []delCount = 0#处理UIL文件,迭代lanFist集合,标记字串放在UIL文件后面
def priorityString():lanFist = []lanList1 = []lanList2 = []lanList3 = []pat = re.compile(".*\"(IDS_String\w*)\".*")for line in open(strUilPath+"/English.uil","r").readlines():if strMark in line and pat.search(line):lanFist.append(pat.search(line).group(1))PrStrfilename = strUiPorject + "优先字串整理.h"open(PrStrfilename, "a").writelines([x + "\n" for x in lanFist])print "优先级字串共%d,如下:" %len(lanFist)print lanFistprint "\n\n优先字串共 %d 个。\n保存在当前目录 %s-文件中\n\n" % (len(lanFist),PrStrfilename)for dirPath, dirNames, fileNames in os.walk(strUilPath):for sourceFile in fileNames:filePath = dirPath + "/" + sourceFilefor line in open(filePath, "r"):if pat.search(line) and pat.search(line).group(1) in lanFist:line = line.replace(strMark,"")lanList1.append(line)elif "IDS_String_spliteLine" in line:lanList3.append(line)else:lanList2.append(line)if(len(lanList1) and len(lanList2)>=3):lanList2 = lanList2[0:-1] +lanList3 + lanList1+lanList2[-1:]lanList3 = []open(filePath, "w").writelines(lanList2)lanList1 = []lanList2 = []#给已整理好优先级高的字串添加strMark
def AddMark():StringList = []a = 0for line in open(strUilPath+"/English.uil","r") :if ("<String ID=" in line):  if re.search("IDS_String_spliteLine",line):a = 1elif(a == 1):line = line.replace('Value="','Value="'+strMark)StringList.append(line)open(strUilPath+"/English.uil","w").writelines(StringList)print "\n添加StrMark完成\n"#读取 优先级字串整理.h 中的字串进行比对添加标记(未整理)
def CompareAddMark():strSet = set()strList = []for line in open('formal优先字串整理.h', "r"):strSet.add(re.search(".*(IDS_String_.*).*",line).group(1))print strSetfor line in open(strUilPath+"/English.uil","r") :if "<String ID=" in line:if re.search("\s*<String ID=\"(\w*)\".*", line).group(1) in strSet:line = line.replace('Value="','Value="'+strMark)strList.append(line)open(strUilPath+"/English.uil","w").writelines(strList)    def fun_parse_InputParam():global g_deleteModeglobal g_priorityModeglobal g_AddmarkModeglobal g_CompareModetry:opts, args = getopt.getopt(sys.argv[1:], 'dpac')except getopt.GetoptError, err:#print str(err)
        sys.exit()for op, value in opts:if op == "-d":g_deleteMode = 1elif op == "-p":g_priorityMode = 1elif op == "-a":g_AddmarkMode = 1elif op == "-c":g_CompareMode = 1else:print("unhandled option")sys.exit()if __name__ == "__main__":fun_parse_InputParam()if g_deleteMode:filterUsefulString()deleteString()if g_priorityMode:priorityString()if g_AddmarkMode:AddMark()if g_CompareMode:CompareAddMark()    

转载于:https://www.cnblogs.com/jiangzhaowei/p/9278403.html

Python 字串处理相关推荐

  1. Python字串(string)基础与20种常见操作

    多数的程式设计师,处理字串的次数远比数字还要多. 程式设计给人的印象通常是会使用到许多数学,也是不少人对学程式语言感到惧怕的原因. 但其实程式设计的实务上,处理文字字串(string)的频率远比数字高 ...

  2. python leetcode_python实现leetcode中无重复字符的最长字串

    ​无重复字符的最长字串是一道经典的额字符串处理算法的题目,日常编程过程中,处理字符串是很多见的.用Python来实现leetcode这道算法题,这题会涉及到一个概念"滑动窗口"的概 ...

  3. 蓝桥杯 基础练习 01字串 C/C++/Java/Python描述

    蓝桥杯 基础练习 01字串 C/C++/Java/Python描述   大家好,我叫亓官劼(qí guān jié ),在CSDN中记录学习的点滴历程,时光荏苒,未来可期,加油~博客地址为:亓官劼的博 ...

  4. python 判断 字串包含_数百道BAT等大厂最新Python面试真题,学到你手软!

    春招临近,无论是要找工作的准毕业生,还是身在职场想要提升自己的程序员,提升自己的算法内功心法.提升 Python 编程能力,总是大有裨益的.今天,小编发现了一份好资源:Python 实现的面试题集锦! ...

  5. zz使用 Python 分离中文与英文的混合字串

    使用 Python 分离中文与英文的混合字串 LiYanrui posted @ 大约 1 年前 in 程序设计 with tags python , 614 阅读 这个问题是做 MkIV 预处理程序 ...

  6. python 分割字串_在Python中分割,连结和连结字串

    python 分割字串 There are few guarantees in life: death, taxes, and programmers needing to deal with str ...

  7. python【蓝桥杯vip练习题库】ADV-150 周期字串

    试题 算法提高 周期字串 资源限制 时间限制:1.0s 内存限制:256.0MB 问题描述 右右喜欢听故事,但是右右的妈妈总是讲一些"从前有座山,山里有座庙,庙里有个老和尚给小和尚讲故事,讲 ...

  8. python【蓝桥杯vip练习题库】ALGO-87字串统计(暴力)

    我得做了两个小时.....我测试了n遍,这么简单的题,我个人对数组太不敏感了,特别是数组边界运算,这个i那个j的不知道大家怎么样....越界是常见操作.还有就是一定要好好读题,一定 比如说特殊的边界, ...

  9. LeetCode中等题之无重复字符的最长字串

    题目 给定一个字符串 s ,请你找出其中不含有重复字符的 最长子串 的长度. 示例 1: 输入: s = "abcabcbb" 输出: 3 解释: 因为无重复字符的最长子串是 &q ...

最新文章

  1. 爬虫之常见的验证码的种类
  2. Centos7修改主机名
  3. @scheduled只执行一次_SpringBoot第四篇:定时任务@Scheduled
  4. 在Eclipse中添加JDK源码包
  5. 代码实现:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。...
  6. 能跳过节假日的闹钟_苏宁小Biu智能闹钟体验:聪明好看,别具风格的“小物件”...
  7. python 并查集_python 实现并查集源代码 | 学步园
  8. android 8 wifi 信号等级
  9. python求解在给定递减数组中寻找两个数和等于定值,乘积最小
  10. Gentoo Portage树服务器(SYNC服务器)的搭建[转]
  11. 网站类项目商业计划书
  12. B站左程云算法笔记06
  13. Flutter开发:使用SafeArea(安全区域)
  14. peer之间的通信协议
  15. MATLAB进行非线性拟合
  16. 苹果MAC AIR MACOS X 10.8安装notes提示OS版本过低
  17. 为什么包装类型间的相等判断应该用 equals
  18. C++ OpenCV(一):图像读取与保存
  19. Appium 点击操作梳理
  20. windows装linux

热门文章

  1. sed for windows 双引号内部内容的替换
  2. xposed学习四:总结
  3. hadoop 学习(1):搭建环境
  4. 引擎设计跟踪(九.6) 地形最近更新
  5. [ios] iOS中arc的设置与使用
  6. iframe内部内容在浏览窗口位置固定的问题
  7. Enterprise Continuous Integration with Binary Dependencies example
  8. 剪枝实践:图像检索如何加速和省显存 ?
  9. Facebook开源算法代码库PySlowFast,轻松复现前沿视频理解模型
  10. CVPR2021系列(一)—— 语义图像抠图