Python Forum
[SOLVED] [Windows] Fails reading strings with accents
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] [Windows] Fails reading strings with accents
#1
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.
Reply
#2
try (untested):
with open('stops.txt') as file1:
    for line in file1:
        line = line1.strip()
        print(f"{line}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 369 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Trying to understand strings and lists of strings Konstantin23 2 778 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  [SOLVED] [Windows] Right way to prompt for directory? Winfried 4 2,065 Jan-17-2023, 09:28 PM
Last Post: markoberk
  [SOLVED] [Windows] Converting filename to UTF8? Winfried 5 2,581 Sep-06-2022, 10:47 PM
Last Post: snippsat
  Splitting strings in list of strings jesse68 3 1,782 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  [Solved] Reading every nth line into a column from txt file Laplace12 7 5,251 Jun-29-2021, 09:17 AM
Last Post: Laplace12
  reading lines from a string [Solved] ebolisa 14 6,427 Mar-28-2021, 08:16 PM
Last Post: perfringo
  Letters with Accents Not Inputting Phylus 0 1,466 Jun-12-2019, 04:05 PM
Last Post: Phylus
  Letters with Accents Not Displaying Phylus 5 5,554 Jun-12-2019, 05:43 AM
Last Post: Phylus
  Finding multiple strings between the two same strings Slither 1 2,534 Jun-05-2019, 09:02 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020