#-*- 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来直接过滤。