[英]Export csv with ISO-8859-1 encoding instead of UTF-8
I struggle with encoding in csv exports.我很难在 csv 导出中进行编码。 I'm from the Netherlands and we use quite some trema's (eg ë
, ï
) and accents (eg é
, ó
) etc. This causes troubles when exporting to csv and open file in excel.我来自荷兰,我们使用了相当多的 trema(例如ë
、 ï
)和重音(例如é
、 ó
)等。这在导出到 csv 并在 excel 中打开文件时会引起麻烦。
On macOS Mojave.在 macOS Mojave 上。
I've tried multiple encoding functions like the following.我已经尝试了多种编码功能,如下所示。
library(stringr)
library(readr)
test <- c("Argentinië", "België", "Haïti")
test %>%
stringi::stri_conv(., "UTF-8", "ISO-8859-1") %>%
write.csv2("~/Downloads/test.csv")
But still, this causes weird characters:但是,这仍然会导致奇怪的字符:
How do I take care of people importing the right encoding in their Excel..?我如何照顾人们在他们的 Excel 中导入正确的编码..?
Don't convert to iso-8859-1 but export with readr::write_excel_csv2()
.不要转换为 iso-8859-1,而是使用readr::write_excel_csv2()
导出。 It writes the file as UTF-8, but with byte order mark (BOM) , which Excel understands).它将文件写入为 UTF-8,但带有 Excel 理解的字节顺序标记 (BOM )。
library(readr)
test <- c("Argentinië", "België", "Haïti")
I need to convert test
to UTF-8, because I am on Windows.我需要将test
转换为 UTF-8,因为我在 Windows 上。
test <- enc2utf8(test)
On MacOS test
should be in UTF-8 already, as that is the native encoding.在 MacOS 上test
应该已经是 UTF-8,因为这是本机编码。
Encoding(test)
#> [1] "UTF-8" "UTF-8" "UTF-8"
Save as an Excel csv with readr::write_excel_csv2()
使用readr::write_excel_csv2()
保存为 Excel csv
write_excel_csv2(data.frame(x = test), "test.csv")
Alternatively save as xlsx
with writexl::write_xlsx()
或者使用writexl::write_xlsx()
保存为xlsx
writexl::write_xlsx(data.frame(x = test), "test.xlsx")
test <- c("Argentinië", "België", "Haïti")
con <- file('~/test.csv', encoding = "ISO-8859-1")
write.csv(x = test, file = con)
A call to csvlook test.csv -e "ISO-8859-1"
in my bash returns | a | x | | - | ---------- | | 1 | Argentinië | | 2 | België | | 3 | Haïti |
在我的 bash 中调用csvlook test.csv -e "ISO-8859-1"
返回| a | x | | - | ---------- | | 1 | Argentinië | | 2 | België | | 3 | Haïti |
| a | x | | - | ---------- | | 1 | Argentinië | | 2 | België | | 3 | Haïti |
do the simple thing做简单的事
Just open the file in note pad and save as UTF -8 in another name, now open the saved notepad file in excel and it will ask you import, do delimiter based on your report and use , also as delimiter for columns separation and finish import.只需在记事本中打开文件并以另一个名称另存为UTF -8,现在在excel中打开保存的记事本文件,它会要求您导入,根据您的报告做定界符并使用,也作为分栏分隔符并完成导入. you will get your clean file你会得到你的干净文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.