最近迷上的Minecraft,遇上了一个名叫“洲际导弹”的mod,但是打开mod里面都是Unicode的代码,用压缩包打开mod里的\assets\icbmclassic\lang\zh_CN.lang看到,原来是编码出了问题:原本想一个个手换的。。。
但是!作为一个程序员,就要有程序员的思想,用了十分钟写了个Python代码(可能手换还更快)
import re
import langconv
def Tradition(sentence):
sentence = sentence.encode('utf-8').decode('unicode_escape')
sentence = langconv.Converter('zh-hans').convert(sentence)
return sentence
if __name__ == '__main__':
with open("zh_CN.lang") as f:
with open("zh_CN_.lang", "w") as f2:
for line in f:
Ucode = re.findall(r"[\\u\w\w\w\w]+$", line)
if len(Ucode) != 0:
temp = Tradition(Ucode[0])
line = line.replace(Ucode[0], temp)
f2.write(line)
(用上了langconv库,因为Unicode转出来是繁体,用它换成简体,这个好像要去github自己下)
效果如图: