pythonutf-8python-2.xnon-ascii-charactersord

Python 2 - ord() returning an incorrect value


This is my text file (sample.txt).

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

Now when I invoke python script to read the ord() values of those characters I always receive 195. Why is that?

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

ord() value of 195 is this character: Ã and it is not present in any of the above-mentioned lines by me at all. I am expecting within an output of the following sequence for:

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

Solution

  • You should switch to python 3; It fixes the problem:

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

    This prints:

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

    Just like expected.