When I'm opening a .txt file with special characters such as ö and ä, they look like this in the .txt file � and like this when I open loop through the lines ¿½. How can I read them with the real special characters? I need to compare strings and if i compare ä == �
it returns False
.
CodePudding user response:
python supports unicode, and in fact python3 uses utf-8 unicode encoding for strings by default. So you should be able to just open up the file and read the content -- special characters would be handled gracefully as they are just normal unicode characters.
For example:
with open('special', 'r') as inf:
content = inf.read()
print(content[0])
$ cat special
ääää
$ python3 read.py
ä
CodePudding user response:
You can try using the "encoding" argument. It worked with me.
with open("text.txt",'r',encoding='utf-8') as f