Python Forum

Full Version: [SOLVED] [Windows] Fails reading strings with accents
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

On Windows, I'm trying to read a file that has accents. An hexdump shows that it's encoded in Latin1/ISO-8859-1, not UTF-8.

For some reason, Python still isn't happy:

# -*- coding: latin-1 -*-

#file1 = open('stops.txt', 'r')
#file1 = open('stops.txt', 'r',encoding="ISO-8859-1")
file1 = open('stops.txt', 'r',encoding="latin-1")
for line in file1.readlines():
	#line = line.strip()
	line.strip()
	#SyntaxError: Non-UTF-8 code starting with '\xf1' in file dummy.py on line 21, but no encoding declared; see http://python.org/dev/peps/pep-0263/
	print(line)
Any idea why?

Thank you.

--
Edit: Problem solved. I had a comment line in the script that had an accent, and the encoding line only works if it's on the very first line, or Python barfs.
try (untested):
with open('stops.txt') as file1:
    for line in file1:
        line = line1.strip()
        print(f"{line}")