Python /从文本文件中读取带重音符号的字符:ord()值始终返回195

问题描述 投票:0回答:1

这是我的文本文件(sample.txt)。

É
Â
Ê
Î
Ç
Ô
È
Û
Ï
Ë
À
Ù
Ü

现在,当我调用python脚本读取那些字符的ord()值时,我总是收到195。为什么?

file = open("C:\sample.txt", "r")
for line in file:
    print ord(line[0])
file.close()

ord()值195是这个字符:Ã,而我在上述任何几行中都没有出现。我期望在以下序列的输出中为:201、194、202、206、199、212、200、219、207、203、192、217、220。

python utf-8 diacritics non-ascii-characters ord
1个回答
0
投票

您应该切换到python 3;它解决了问题:

file = open("sample.txt", "r")
for line in file:
    print(ord(line[0]))
file.close()

此打印:

201
194
202
206
199
212
200
219
207
203
192
217
220

就像预期的一样。

© www.soinside.com 2019 - 2024. All rights reserved.