求大神!2进制导出文本语言显示不正确
2进制导出文本语言 中文 日文 韩文和一些符号都显示不正确
# -*- coding: utf-8 -*-
import os
import json
import codecs
def read_block(f):
block_size = 0
index = int.from_bytes(bytes(f.read(4)), byteorder='little')
block_size += 4
len1 = int.from_bytes(bytes(f.read(2)), byteorder='little')
block_size += 2
str1 = codecs.decode(bytes(f.read(len1 * 2)), encoding='utf-8', errors='replace').replace('\00','')
block_size += len1 * 2
return {"index": index, "str1": str1, "size": block_size}
fname = 'tb_npc_script.res'
fsize = os.path.getsize(fname)
entries = []
with open("tb_npc_script.res", "rb") as f:
try:
entry_count = int.from_bytes(f.read(4), byteorder='little')
fsize -= 4
while entry_count > 0:
block = read_block(f)
fsize -= block["size"]
entry_count -= 1
entries.append(block)
except Exception as e:
print(e)
with open('tb_npc_script.txt', 'w', encoding='utf-8') as c:
json.dump(entries, c, ensure_ascii=False, indent=4)
print('完成')