Solving Broken Characters when Exporting CSV in Julia
Error
using DataFrames, CSV
example = DataFrame(x = 1:10, 가 = "나다")
CSV.write("example.csv", example)
When outputting to a CSV file in Julia, you can see a phenomenon where the Korean text becomes garbled, as shown above.
Cause
The garbling isn’t actually due to the Korean text itself but a Unicode encoding issue, especially due to the UTF-8 encoding’s BOM. This can be solved by setting the encoding to UTF-8-sig in Python, among others.
Solution 1
CSV.write("example.csv", example, bom = true)
In CSV.jl
, simply setting the option bom = true
will output the text without it becoming garbled, as seen below.
Environment
- OS: Windows
- julia: v1.6.3