Search code examples
pythonfile-handling

Read text file outputs incorrect data


Original question

I have been trying to sort a list of words based off a custom alphabet: æǽaàbdyýeéfgiíʒklmnŋΩΏoóøǿɤprʃsþðtʊuúʌvɯjz . The program was outputting random nonsense that I didn't put in, and I decided it was a problem with reading the file.

When I read the text file:

mægnʌ
d
d

gløsΩs
d
d

I get:

magnʌ
d
d

gløsΩs
d
d

And it causes an error later in the program.


Solution

  • If the characters in your custom alphabet are a part of Unicode, then you can open the file with ‘UTF-8’ encoding, like @OM222O suggested. Modified code:

    with open(YOUR_FILEPATH, encoding='UTF-8') as file:
        print(file.readlines())
    

    You have to replace YOUR_FILEPATH with your file path. Using the encoding will ensure that the characters are printed correctly. The ‘with’ statement is an efficient way of opening a file because the file is closed on exiting the ‘with’ statement.

    PS: I think your alphabet is a Nordic alphabet, so Unicode encoding should work pretty well.