在 R 中将 UTF-8 BOM 导出到 .csv [英] Export UTF-8 BOM to .csv in R
问题描述
我正在通过 RJDBC 从 MySQL 数据库读取文件,它正确显示了 R 中的所有字母(例如,נווה שאנן).但是,即使使用 write.csv 和 fileEncoding="UTF-8" 导出它,输出看起来也像<代码>..<U+041B><U+043E><U+0437><U+0435><U+043D><U+0435><U+0446>(在这种情况下这不是上面的字符串,而是保加利亚语字符串)用于保加利亚语、希伯来语、中文等.ã、ç 等其他特殊字符也可以正常工作.
I am reading a file through RJDBC from a MySQL database and it correctly displays all letters in R (e.g., נווה שאנן).
However, even when exporting it using write.csv and fileEncoding="UTF-8" the output looks like
<U+0436>.<U+043A>. <U+041B><U+043E><U+0437><U+0435><U+043D><U+0435><U+0446>
(in this case this is not the string above but a Bulgarian one) for Bulgarian, Hebrew, Chinese and so on. Other special characters like ã,ç etc work fine.
我怀疑这是因为 UTF-8 BOM 但我没有在网上找到解决方案
I suspect this is because of UTF-8 BOM but I did not find a solution on the net
我的操作系统是德国的 Windows7.
My OS is a German Windows7.
我试过
con<-file("file.csv",encoding="UTF-8")
write.csv(x,con,row.names=FALSE)
和(afaik)等效的write.csv(x, file="file.csv",fileEncoding="UTF-8",row.names=FALSE)
.
and the (afaik) equivalent write.csv(x, file="file.csv",fileEncoding="UTF-8",row.names=FALSE)
.
推荐答案
On help page to Encoding
(help("Encoding")
) you can read about special encoding- 字节
.
On help page to Encoding
(help("Encoding")
) you could read about special encoding - bytes
.
使用它,我能够通过以下方式生成 csv 文件:
Using this I was able to generate csv file by:
v <- "נווה שאנן"
X <- data.frame(v1=rep(v,3), v2=LETTERS[1:3], v3=0, stringsAsFactors=FALSE)
Encoding(X$v1) <- "bytes"
write.csv(X, "test.csv", row.names=FALSE)
注意factor
和character
之间的差异.以下应该有效:
Take care about differences between factor
and character
. The following should work:
id_characters <- which(sapply(X,
function(x) is.character(x) && Encoding(x)=="UTF-8"))
for (i in id_characters) Encoding(X[[i]]) <- "bytes"
id_factors <- which(sapply(X,
function(x) is.factor(x) && Encoding(levels(x))=="UTF-8"))
for (i in id_factors) Encoding(levels(X[[i]])) <- "bytes"
write.csv(X, "test.csv", row.names=FALSE)
这篇关于在 R 中将 UTF-8 BOM 导出到 .csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!