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
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 758 Nov-04-2025, 11:51 AM
Last Post: buran
  Bug: Two Python unistall strings available on Windows 10 pstein 2 623 Oct-26-2025, 11:24 AM
Last Post: noisefloor
Question [DOS/cmd] How to handle accents? Winfried 5 586 Oct-24-2025, 01:10 PM
Last Post: buran
Question [SOLVED] [Windows] Change directory date? Winfried 2 1,413 May-11-2025, 04:24 PM
Last Post: Winfried
Question [SOLVED] [Windows] Is this right way to run a CLI app? Winfried 2 1,636 Nov-28-2024, 10:01 AM
Last Post: Winfried
  [SOLVED] Pad strings to always get three-digit number? Winfried 2 1,901 Jan-27-2024, 05:23 PM
Last Post: Winfried
  Trying to understand strings and lists of strings Konstantin23 2 2,370 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  [SOLVED] [Windows] Right way to prompt for directory? Winfried 4 3,940 Jan-17-2023, 09:28 PM
Last Post: markoberk
  [SOLVED] [Windows] Converting filename to UTF8? Winfried 5 6,511 Sep-06-2022, 10:47 PM
Last Post: snippsat
  Splitting strings in list of strings jesse68 3 3,211 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.