Script to convert ANSI to UTF-8

238 Views Asked by At

I have an application that writes ANSI-encoded CSV files to a file share, but the application I use to read them expects UTF-8.

Neither application has the option of changing the encoding, so I have been doing this manually. I have been trying to find a script online that I can use as a scheduled task to convert these files.

Is this even possible? If so can anyone suggest a script.

Right now I have only tried converting manually as I am not sure how to script this.

1

There are 1 best solutions below

3
Zach Young On

If you can use Python, the following should get you on the right track.

Presuming that ANSI means Windows-1252, I believe the following script will only change the encoding and leave the "data" as-is.

If the input encoding is something other than Windows-1252 (e.g., 'cp437', 'windows-1254'), just replace the input encoding below:

f_in = open("input.csv", encoding="windows-1252")
f_out = open("output.csv", "w", encoding="utf-8")

for line in f_in:
    f_out.write(line)

f_out.close()
f_in.close()

I made up this input.csv and saved it as Windows-1252:

Col1, Col2
ÀÁÂ,  àáâ
ÈÉÊ,  èéê
ÍÎÏ,  íîï
ÔÕÖ,  ôõö
ÚÛÜ,  úûü

ran that script, and output.csv looks correct.