[Python] 纯文本查看 复制代码 import chardet
import os
import re
from shutil import copyfile
import json
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.tmt.v20180321 import tmt_client, models
import time
# 环境
"""
tencentcloud-sdk-python==3.0.871
tencentcloud-sdk-python-tmt==3.0.871
chardet==5.1.0
"""
print("请输入腾讯翻译秘钥,没有的可以去免费申请(百度有教程)")
token = credential.Credential(input("secret_id:"), input("secret_key:")
)
def transl(array):
# 实例化一个http选项,可选的,没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "tmt.tencentcloudapi.com"
# 实例化一个client选项,可选的,没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的
client = tmt_client.TmtClient(token, "ap-guangzhou", clientProfile)
# 实例化一个请求对象,每个接口都会对应一个request对象
req = models.TextTranslateBatchRequest()
params = {
"Source": "auto",
"Target": "zh",
"ProjectId": 0,
"SourceTextList": array
}
req.from_json_string(json.dumps(params))
resp = client.TextTranslateBatch(req)
# 输出json格式的字符串回包
return resp.TargetTextList
def find_info_extract(path):
for root, dirs, files in os.walk(path):
for file in files:
if file == 'mod.info':
file_name = os.path.join(root, file)
if os.path.exists(file_name+".backup") == False:
copyfile(file_name, file_name+".backup")
# ----备份源文件
else: # 恢复源文件
copyfile(file_name+".backup", file_name)
# break
with open(file_name, 'rb') as f:
data = f.read()
encoding = chardet.detect(data)['encoding']
ascii_data = data.decode(
encoding) # 写出 ascii_data.encode('utf-8', 'ignore')
matches = re.findall(r'(.*\S)\s*=\s*(.*)$', ascii_data, re.M)
enter = []
index = 0
for match in matches:
if match[0] == "name" or match[0] == "description":
enter.append(index)
index = index+1
tl = transl([list(matches)[_][1] for _ in enter])
index = 0
for _ in enter:
content = list(matches[_])
content[1] = (tl[index]+" | "+content[1]
).replace('\r', '')+"\r"
matches[_] = tuple(content)
index += 1
...
export = None
for match in matches:
if match[0] == "name" or match[0] == "description":
enter.append(index)
if export == None:
export = match[0]+"="+match[1]
else:
export = export+"\n"+match[0]+"="+match[1]
index = index+1
print(root, '\n', tl, '\n')
with open(file_name, 'wb') as f:
f.write(export.encode('utf-8', 'ignore'))
time.sleep(0.3)
print("示例(...Steam/steamapps/workshop/content/108600)")
find_info_extract(input("模组路径:"))
|