为什么'é'和'é'编码为不同的字节?

时间:2016-09-20 00:31:04

标签: python python-3.x unicode normalization

问题

为什么在我的代码库的不同部分中,相同的字符编码为不同的字节?

上下文

我有一个单元测试,它会生成一个临时文件树,然后检查以确保我的扫描实际上找到了有问题的文件。

def test_unicode_file_name():
    test_regex = "é"
    file_tree = {"files": ["é"]} # File created with python.open()
    with TempTree(file_tree) as tmp_tree:
        import pdb; pdb.set_trace()
        result = tasks.find_files(test_regex, root_path=tmp_tree.root_path)
        expected = [os.path.join(tmp_tree.root_path, "é")]
        assert result == expected

失败的功能

for dir_entry in scandir(current_path):
    if dir_entry.is_dir():
        dirs_to_search.append(dir_entry.path)

    if dir_entry.is_file():
        testing = dir_entry.name
        if filename_regex.match(testing):
            results.append(dir_entry.path)

PDB会话

当我开始挖掘事物时,我发现测试字符(从我的单元测试中复制)和dir_entry.name中的字符编码为不同的字节。

(Pdb) testing
'é'
(Pdb) 'é'
'é'
(Pdb) testing == 'é'
False
(Pdb) testing in 'é'
False
(Pdb) type(testing)
<class 'str'>
(Pdb) type('é')
<class 'str'>
(Pdb) repr(testing)
"'é'"
(Pdb) repr('é')
"'é'"
(Pdb) 'é'.encode("utf-8")
b'\xc3\xa9'
(Pdb) testing.encode("utf-8")
b'e\xcc\x81'

1 个答案:

答案 0 :(得分:3)

您的操作系统(MacOS,猜测)已将文件名'é'转换为Unicode Normal Form D,将其分解为无重音'e'和组合的重音符号。您可以通过Python解释器中的快速会话清楚地看到这一点:

>>> import unicodedata
>>> e1 = b'\xc3\xa9'.decode()
>>> e2 = b'e\xcc\x81'.decode()
>>> [unicodedata.name(c) for c in e1]
['LATIN SMALL LETTER E WITH ACUTE']
>>> [unicodedata.name(c) for c in e2]
['LATIN SMALL LETTER E', 'COMBINING ACUTE ACCENT']

为了确保您比较喜欢,您可以将dir_entry.name给出的文件名转换回Normal Form C,然后再针对您的正则表达式进行测试:

import unicodedata

for dir_entry in scandir(current_path):
    if dir_entry.is_dir():
        dirs_to_search.append(dir_entry.path)

    if dir_entry.is_file():
        testing = unicodedata.normalize('NFC', dir_entry.name)
        if filename_regex.match(testing):
            results.append(dir_entry.path)