17

I'm making a parser (1 csv to 3 csv) script and I have a problem. I am French so in my language I have letters like: é è à ....

A customer sent me a csv file that Linux recognizes as "unknown-8bit" (ansi I guess).

In my script, I'm writing 3 new csv files. But ViM creates them as ISO latin1 because it's close to what it got in the entry, but my é,è,à... are broken. I need UTF-8.

So I tried to convert the first ANSI csv to UTF-8 :

iconv -f "windows-1252" -t "UTF-8" import.csv -o import.csv

The problem is that it breaks my CSV. It's now on only one row. But my special chars are ok. Is there a way to convert ANSI to UTF-8 and keeping my rows?

1 Answer 1

39

Put the output into another file. Don't overwrite the old one.

iconv -f "windows-1252" -t "UTF-8" import.csv -o new_import.csv

iconv fails when reading and writing to the same file.

3
  • 6
    Use sponge from moreutils (additional Unix utilities) to keep the same name like so: iconv -f "windows-1252" -t "UTF-8" import.csv | sponge import.csv
    – ndemou
    Dec 26, 2015 at 20:23
  • I needed to convert Czech subtitles, so I had to use: CP1250 as input encoding.
    – To Kra
    Dec 28, 2016 at 13:39
  • for me worked the following iconv -f "windows-1252" -t "UTF-8" import.csv > new_import.csv Mar 8, 2021 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.