python3 自定义编码 mik-保加利亚语
python3 custom encoding mik-bulgarian
我正在尝试使用 python 3.8 解码一个采用 MIK-BULGARIAN 编码 https://en.wikipedia.org/wiki/MIK_(character_set) 的文件。它是一种与 ASCII 相同的编码,但字节 128-191 是西里尔字母。该文件同时包含拉丁字母和西里尔字母。
我当前的解决方案运行良好,但处理大文件时速度相当慢。你能给我一些如何加快速度的建议吗(我知道这是一种伐木工人的方法,我愿意接受建议)。
def opener(filename):
f = open(filename, "rb")
filetext = f.read()
cadText = translate(filetext)
f.close()
return cadText
mikdict = {
128: "А",
129: "Б",
130: "В",
131: "Г",
132: "Д",
....
188: "ь",
189: "э",
190: "ю",
191: "я"
}
def translate(textbytes):
goodText = ""
for txtbyte in textbytes:
if (txtbyte >= 128) and (txtbyte <= 191):
letter = str(mikdict.get(txtbyte))
else:
letter = chr(txtbyte)
goodText = goodText + letter
[code]显然正确的答案是使用 map() 和 lambda,因为它似乎比我最初的代码片段更有效。
def translate(input):
newChars = map(lambda x: bytes([x]) if (x < 128) else bytes(mik.mikdict.get(x), "utf-8") if (x <= 191) and (x >= 128) else b"", input)
res = b''.join(newChars).decode("utf-8")
return res
我正在尝试使用 python 3.8 解码一个采用 MIK-BULGARIAN 编码 https://en.wikipedia.org/wiki/MIK_(character_set) 的文件。它是一种与 ASCII 相同的编码,但字节 128-191 是西里尔字母。该文件同时包含拉丁字母和西里尔字母。 我当前的解决方案运行良好,但处理大文件时速度相当慢。你能给我一些如何加快速度的建议吗(我知道这是一种伐木工人的方法,我愿意接受建议)。
def opener(filename):
f = open(filename, "rb")
filetext = f.read()
cadText = translate(filetext)
f.close()
return cadText
mikdict = {
128: "А",
129: "Б",
130: "В",
131: "Г",
132: "Д",
....
188: "ь",
189: "э",
190: "ю",
191: "я"
}
def translate(textbytes):
goodText = ""
for txtbyte in textbytes:
if (txtbyte >= 128) and (txtbyte <= 191):
letter = str(mikdict.get(txtbyte))
else:
letter = chr(txtbyte)
goodText = goodText + letter
[code]显然正确的答案是使用 map() 和 lambda,因为它似乎比我最初的代码片段更有效。
def translate(input):
newChars = map(lambda x: bytes([x]) if (x < 128) else bytes(mik.mikdict.get(x), "utf-8") if (x <= 191) and (x >= 128) else b"", input)
res = b''.join(newChars).decode("utf-8")
return res