这是我的文本文件(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 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
就像预期的一样。