How to Fix UTF-8 CSV Encoding Issues in Excel on macOS


It may happen that correctly encoded in UTF-8 CSV file, when opened in Excel, shows diacritics in a wrong way. Excel does not assume UTF-8 unless a BOM is present, so it falls back to a legacy encoding and misinterprets bytes. Let me show you a quick tip, how to help Excel on macOS open such file correctly.

Problem

CSV file before the fix. Polish diacritics are completely broken.

Solution

We can fix the problem with an easy to execute one-liner command in macOS terminal. Assuming your file is called products.csv, the following command will fix it:

printf '\xEF\xBB\xBF' | cat - products.csv > products-fixed.csv

Explain this magic please…

Sure

  1. We print UTF-8 BOM (Byte Order Mark) EF BB BF (this is basically what Excel needs to detect UTF-8 enconding)
  2. We pipe the output from left command and pass it as input to the next command (the one after | operator)
  3. Take BOM from printf and append the contents ofΒ products.csv to it
  4. Finally we redirect output to new file called products-fixed.csv

Preview

CSV file after the fix. Polish diacritics are fully readable.

This is it.

Β 


Words: 195
Published in: macOS

Related Articles