python 提取lua文件中的中文

#-*- coding: UTF-8 -*-

import os

# 遍歷指定目錄,顯示目錄下的所有文件名
def eachFile(filepath):
    for root,dirs,files in os.walk(filepath):
        for file in files:
            luaFileName = os.path.join(root,file)
            readFile(luaFileName)

# 讀取文件內容並打印
def readFile(filename):
    index=  filename.find(".lua")
    if index <= 0:
        return

    print filename

    fopen = open(filename, 'r')  # r 代表read
    try:
        all_the_text = fopen.read()
        chinese = ""
        dataLen = len(all_the_text)
        i = 0
        while i < dataLen:
            value = ord(all_the_text[i])
            if  value == 34 and i+1 < dataLen:
                i = i + 1
                while ord(all_the_text[i]) != 34 and i+1 < dataLen:
                    chinese =  chinese+all_the_text[i]
                    i = i + 1
                if isCanShow(chinese) == True:
                    print  chinese

                chinese = ""
            i =  i + 1;
    finally:
        fopen.close()
    print("end read file")

#全部ASCII碼,不需要顯示
def isCanShow(str):
    flag = False
    tick = 0
    for cha  in str:
        value = ord(cha)
        if value <= 127:
            tick = tick + 1
    if tick == len(str):
        return False
    return  True

if __name__ == '__main__':
    filePathC = "D:\\git\\client3\\src"
    eachFile(filePathC)
    #readFile(filePath)
    #writeFile(filePathI)

 

項目需求:

       遊戲製作多國語言版本,把LUA代碼中所有的文字抽出來。

      代碼中用到文字的都是 雙引號(“)開頭跟結尾的。所以只提取這部分,另外圖片跟資源也是 雙引號(“)開頭跟結尾的,所以用了isCanShow來直接過濾。

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章