python 提取lua文件中的中文

2 篇文章 0 订阅
#-*- 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来直接过滤。

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值