Skip to main content
 首页 » 编程设计

python之损坏的希伯来语 : saved as ansi之covert back to UTF-8

2025年05月04日9mayingbao

我怀疑某些数据已(在 Windows 机器上)保存为 ANSI。 因此,原来的希伯来字符丢失了,我们看到的是这样的东西 ùéôåãé äòéø。

信息是否丢失或是否有可能在知道原文是希伯来文的情况下映射回字符?

请您参考如下方法:

信息可能没有丢失,或者至多部分丢失。如果你想使用 Python:

import codecs 
BLOCKSIZE = 1048576 # or some other, desired size in bytes 
with codecs.open("input.txt", "r", "windows-1255") as sourceFile: 
    with codecs.open("output.txt", "w", "utf-8") as targetFile: 
        while True: 
            contents = sourceFile.read(BLOCKSIZE) 
            if not contents: 
               break 
            targetFile.write(contents) 

盗用并改编自 How to convert a file to utf-8 in Python?

您还可以使用外部工具,例如 iconv:

iconv -f windows-1255 -t utf-8 input.txt > output.txt 

Iconv 在大多数 Linux 发行版、Cygwin 和其他平台上都可用。

如果文件被双重破坏,你可能需要做这样的事情:

iconv -f utf-8 -t windows-1252 input.txt > tmp.txt 
iconv -f windows-1255 -t utf-8 tmp.txt > output.txt 

但这种事情发生的可能性微乎其微。