Python3: 列表中的字符串根据访问方式具有不同的编码

2024-10-04 07:39:10 发布

您现在位置:Python中文网/ 问答频道 /正文

这里有点奇怪。我有一个相邻列表,键中有umlauts。但列表中的编码似乎有所不同,当我遍历它时:

adjacency_list = {
    'root':            ['zuw\xc3\xa4chse', 'test2'],
    'zuw\xc3\xa4chse': ['test3', 'test4']
}

print(adjacency_list['root'])
for child in adjacency_list['root']: print(child)

输出:

['zuw\xc3\xa4chse', 'test2']
zuwächse
test2

为什么列表中有'zuw\xc3\xa4chse',但如果我有for child in...child现在是'zuwächse'?为什么编码会不同?我需要相同的编码,这样我就可以使用child的值作为键

编辑

多亏了Padraic Cunningham:如果你打印一些东西,它就会通过repr。这意味着键没有改变,只是它的视觉表现。结果发现这与我遇到的实际问题无关,而且完全是红色的


Tags: inchild编码列表forrootlistprint