Python Forum
[SOLVED] Correct way to convert file from cp-1252 to utf-8?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Correct way to convert file from cp-1252 to utf-8?
#3
The two lines you mean?
#data = open(file, "r").read()
#data.encode(encoding = 'UTF-8', errors = 'strict')
data = rawdata.decode(encoding=encoding)
#with open(file, 'w', encoding='utf-8') as outp:
with open(file, 'wb') as outp:
        outp.write(data) #TypeError: a bytes-like object is required, not 'str'
CHECK LATER It still doesn't work: Files that were supposedly converted to utf-8 in the first run are still considered as Windows files:
if encoding in ["Windows-1252","ascii","ISO-8859-1"]:
  print("File still not in utf-8",file)
  continue
Also, the code above adds new carriage returns in the output :-/

<head>

<title>my title</title>

<meta name="description" content="my title">

<meta name="keywords" content="my title">

<meta name="classification" content="windows">

</head>
Reply


Messages In This Thread
RE: Correct way to convert file from cp-1252 to utf-8? - by Winfried - Feb-26-2024, 03:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,547 Oct-17-2024, 01:15 AM
Last Post: Winfried
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,811 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
  Extracting the correct data from a CSV file S2G 6 1,820 Jun-03-2024, 04:50 PM
Last Post: snippsat
  Convert File to Data URL michaelnicol 3 2,675 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  Python Script to convert Json to CSV file chvsnarayana 8 4,738 Apr-26-2023, 10:31 PM
Last Post: DeaD_EyE
  Loop through json file and reset values [SOLVED] AlphaInc 2 5,470 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
Thumbs Up Convert an Interger into any base !? [Solved] SpongeB0B 8 3,016 Jan-16-2023, 10:24 AM
Last Post: SpongeB0B
  Convert Excel file into csv with Pipe symbol.. mg24 4 2,283 Oct-18-2022, 02:59 PM
Last Post: Larz60+
  Need Help: Convert .pcl file to .pdf file ManuRaval 6 4,048 Sep-13-2022, 01:31 PM
Last Post: ManuRaval
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 4,396 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy

Forum Jump:

User Panel Messages

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