问题描述
我很难在csv导出中进行编码。我来自荷兰,我们使用了很多曲调(例如ë
,ï
)和重音符号(例如é
,ó
)等。这会导致在导出到csv并在excel中打开文件时遇到麻烦。
在macOS Mojave上。
我尝试了以下多种编码功能。
库(字符串)
库(读取器)
测试<-c(Argentinië,België,Haïti)
test%>%
stringi :: stri_conv(。, UTF-8, ISO-8859-1)%&%;%
write.csv2( 〜/ Downloads / test.csv)
但是,这仍然会导致奇怪的字符:
不转换为iso-8859-1,而是使用 readr :: write_excel_csv2()
导出。它将文件写为UTF-8,但带有
I struggle with encoding in csv exports. I'm from the Netherlands and we use quite some trema's (e.g. ë
, ï
) and accents (e.g. é
, ó
) etc. This causes troubles when exporting to csv and open file in excel.
On 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:
Don’t convert to iso-8859-1 but export with readr::write_excel_csv2()
. It writes the file as UTF-8, but with byte order mark (BOM), which Excel understands).
library(readr)
test <- c("Argentinië", "België", "Haïti")
I need to convert test
to UTF-8, because I am on Windows.
test <- enc2utf8(test)
On MacOS test
should be in UTF-8 already, as that is the native encoding.
Encoding(test)
#> [1] "UTF-8" "UTF-8" "UTF-8"
Save as an Excel csv with readr::write_excel_csv2()
write_excel_csv2(data.frame(x = test), "test.csv")
Alternatively save as xlsx
with writexl::write_xlsx()
writexl::write_xlsx(data.frame(x = test), "test.xlsx")
这篇关于使用ISO-8859-1编码而不是UTF-8导出csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!