Write.csv用日语从R到excel

时间:2017-11-23 01:16:22

标签: r cjk

当我使用write.csv作为我的日文文本时,我在Excel中得到了胡言乱语(通常处理日语很好)。我搜索过这个网站寻求解决方案,但是空手而归。是否有一个编码命令要添加到write.csv以使Excel能够从R中正确导入日语?任何帮助赞赏! 谢谢!

3 个答案:

答案 0 :(得分:0)

作为一种解决方法 - 并且诊断 - 您是否尝试保存为.txt,然后在Excel中打开文件并将数据从文本编辑器粘贴到Excel中?

答案 1 :(得分:0)

我刚刚遇到了完全相同的问题 - 我使用了我在网上看到的:

write.csv(merch_df, file = "merch.reduced.csv", fileEncoding = "UTF-8")

确实,在打开我的 .xls 文件时, 等等......奇怪而令人失望。

一点点谷歌,我发现了 Kevin Ushey 写的这篇很棒的博客文章,它解释了这一切......https://kevinushey.github.io/blog/2018/02/21/string-encoding-and-r/

使用他提议的函数:

write_utf8 <- function(text, f = tempfile()) {

# step 1: ensure our text is utf8 encoded
utf8 <- enc2utf8(text)

# step 2: create a connection with 'native' encoding
# this signals to R that translation before writing
# to the connection should be skipped
con <- file(f, open = "w+", encoding = "native.enc")

# step 3: write to the connection with 'useBytes = TRUE',
# telling R to skip translation to the native encoding
writeLines(utf8, con = con, useBytes = TRUE)

# close our connection
close(con)

# read back from the file just to confirm
# everything looks as expected
readLines(f, encoding = "UTF-8")

}

有魔法。谢谢凯文!

答案 2 :(得分:0)

我遇到了与 tchevrier 相同的问题。使用 write.csv 导出时,日语文本无法在 Excel 和文本编辑器中正确显示。我发现使用:

readr::write_excel_csv(df, "filename.csv")

更正了问题。