Saving utf-8 text as ansi file in python

58 Views Asked by At
with open("C:/temp/Python/infile.csv", "r", encoding="utf-8") as csv:
    for line in csv:
        line = line.strip()
        cells = line.split(";")

        lastname = cells[49] 
        street_2nd_line = cells[52]
        city = cells[54]        

        #Convert to XML in ANSI
        with open("C:/temp/Python/outfile.txt", "w", encoding="ansi") as order:
            text =  lastname + "\n" + street_2nd_line + "\n" + city 
            print(text)
            order.write(text)

Hello,

I am opening a CSV file in UTF-8 and trying to store it to another file as ANSI. It works fine for German letters (I work on an German Windows). But if Python finds Slovenian characters, it stops at the "order.write" line with message

return mbcs_encode(input, self.errors)[0]
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character

If I try to do it that way with codecs:

with codecs.open("C:/temp/Python/infile.csv", "r", encoding="utf-8") as csv: and with codecs.open("C:/temp/Python/outfile.txt", "w", encoding="ansi") as order:

than I get the message:

File "", line 721, in write File "", line 377, in write UnicodeEncodeError: 'mbcs' codec can't encode characters in position 0--1: invalid character

If I change the target encoding from ansi to cp1252, I get the error:

File "C:\Users\USER\AppData\Local\Programs\Python\Python312\Lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnicodeEncodeError: 'charmap' codec can't encode character '\u010c' in position 9: character maps to

The printout print(text) to the python shell works properly:

Testuser Čergiu D.Š. : 12345678 Hoče

The problem occurs only by trying to save it.

How can I convert and store it properly?

0

There are 0 best solutions below