本文介绍了使用ISO-8859-1编码而不是UTF-8导出csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 21:10