我正在尝试读取一个文件,但我无法确定字符编码。文件中有两个字符的值我是知道的,我在十六进制编辑器中看到的十六进制值如下:
0xCCA9 é
0xCCBB ê
0xCCC1 á
你有什么想法,这是哪种编码方式?
文件中的所有英文字符都是ASCII编码的。我曾经有过类似的文件,它们是使用Mac Central Europe编码的,如果有用的话,也许它们被意外地编码了多次。
编辑:
在Python 2.7中查找映射的代码:(请参见上面Esailija的答案)。
find_mappings(...)
是一个生成器,它接收一个字符映射字典。它遍历所有可用的字符集,并产生符合条件的字符集。
import pkgutil
import encodings
def get_encodings():
false_positives = set(["aliases"])
found = set(name for imp, name, ispkg in pkgutil.iter_modules(encodings.__path__) if not ispkg)
found.difference_update(false_positives)
return found
def find_mappings(maps):
encodings = sorted(get_encodings())
for f in encodings:
for g in encodings:
try:
if all([k.decode(f).encode(g) == v for k,v in maps.items()]):
yield (f,g)
except:
# Couldn't encode/decode
pass
for mapping in find_mappings({'\xCC': '\xC3', '\xBB': '\xAA', '\xA9': '\xA9', '\xC1': '\xA1'}):
print(mapping)