Python Forum
Right way to open files with different encodings?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Right way to open files with different encodings?
#1
Question 
Hello,

Some of the files could be Windows (latin1, iso9959-1, cp1252), others could be utf-8.

Is try/except the right way to do it?

#with open(file, 'r') as f:
#with open(file, 'r',encoding='utf-8') as f:
#latin1, iso9959-1, cp1252
with open(file, 'r',encoding='latin-1') as f:
  content_text = f.read()

  soup = BeautifulSoup(content_text, 'html.parser')
Thank you.
Reply


Messages In This Thread
Right way to open files with different encodings? - by Winfried - Apr-23-2024, 08:49 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Open files in an existing window instead of new Kostov 2 404 Apr-13-2024, 07:22 AM
Last Post: Kostov
  open python files in other drive akbarza 1 752 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  How to open/load image .tiff files > 2 GB ? hobbyist 1 2,508 Aug-19-2021, 12:50 AM
Last Post: Larz60+
  Open and read multiple text files and match words kozaizsvemira 3 6,828 Jul-07-2021, 11:27 AM
Last Post: Larz60+
Question (solved) open multiple libre office files in libre office lucky67 5 3,440 May-29-2021, 04:54 PM
Last Post: lucky67
  Can't open files Lass86 5 2,525 Nov-10-2020, 07:18 PM
Last Post: jefsummers
  Using Python to loop csv files to open them Secret 4 2,802 Sep-13-2020, 11:30 AM
Last Post: Askic
  Find specific subdir, open files and find specific lines that are missing from a file tester_V 8 3,721 Aug-25-2020, 01:52 AM
Last Post: tester_V
  ModuleNotFoundError: no module named 'encodings' grunge10111 1 3,876 May-29-2020, 02:22 AM
Last Post: Larz60+
  subprocess.Popen() and encodings voltron 0 5,807 Feb-20-2020, 04:57 PM
Last Post: voltron

Forum Jump:

User Panel Messages

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