Python 字串处理

 

#!/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 = 0

setStr = 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" % strUiPorject
    strUilPath = "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+"/"+sourceFile
                if (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 sourceFile
                                print "\n"
                                continue
                            if 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

    #写EXCEL
    wboot = xlwt.Workbook()
    sheet = wboot.add_sheet("Language")
    #操作第一行,抬头
    for col in range(len(listFirst)):
        sheet.write(0,col,listFirst[col])

    #其它行
    row = 1
    for (k,v) in mapStr.items():
        for col in range(len(v)):
            sheet.write(row,col,v[col])
        row = row + 1
    wboot.save(r'Language_.xls')
    
        
'''
#处理UIL文件,对比setStr集合,删除无用字串
def deleteString():
    delCount = 0
    lanList = []
    for dirPath,dirNames,fileNames in os.walk(strUilPath):
        for sourceFile in fileNames:
            filePath = dirPath + "/" + sourceFile
            for line in open(filePath,"r"):
                #==============================================#
                #有些字串在code中是以偏移量的方式使用,不能删除
                if "IDS_String_GMT_" in line:
                    lanList.append(line)
                    continue
                elif 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+1
                else:
                    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 lanFist
    print "\n\n优先字串共 %d 个。\n保存在当前目录 %s-文件中\n\n" % (len(lanFist),PrStrfilename)
    
    for dirPath, dirNames, fileNames in os.walk(strUilPath):
        for sourceFile in fileNames:
            filePath = dirPath + "/" + sourceFile
            for 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 = 0
    for line in open(strUilPath+"/English.uil","r") :
        if ("<String ID=" in line):  
            if re.search("IDS_String_spliteLine",line):
                a = 1
            elif(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 strSet
    for 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_deleteMode
    global g_priorityMode
    global g_AddmarkMode
    global g_CompareMode
    try:
        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 = 1
        elif op == "-p":
            g_priorityMode = 1
        elif op == "-a":
            g_AddmarkMode = 1
        elif op == "-c":
            g_CompareMode = 1
        else:
            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

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值