使用UTF-8编码的read.csv() [英] read.csv() with UTF-8 encoding

查看:510
本文介绍了使用UTF-8编码的read.csv()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从csv文件中读取数据,并将字符编码指定为UTF-8.通过阅读?read.csv()指令,似乎将fileEncoding设置为UTF-8即可完成此操作,但是,在检查时我看不到这一点.导入数据时,是否有更好的方法将字符串的编码指定为UTF-8?

I am trying to read in data from a csv file and specify the encoding of the characters to be UTF-8. From reading through the ?read.csv() instructions, it seems that fileEncoding set equal to UTF-8 should accomplish this, however, I am not seeing that when checking. Is there a better way to specify the encoding of character strings to be UTF-8 when importing the data?

样本数据:

在此处下载示例数据

fruit<- read.csv("fruit.csv", header = TRUE, fileEncoding = "UTF-8")
fruit[] <- lapply(fruit, as.character)
Encoding(fruit$Fruit)

输出为未知",但我希望它为"UTF-8".确保所有导入的字符均为UTF-8的最佳方法是什么?谢谢你.

The output is "uknown" but I would expect this to be "UTF-8". What is the best way to ensure all imported characters are UTF-8? Thank you.

推荐答案

fruit       <- read.csv("fruit.csv", header = TRUE)
fruit[]     <- lapply(fruit, as.character)
fruit$Fruit <- paste0(fruit$Fruit, "\xfcmlaut") # Get non-ASCII char and jam it in!
Encoding(fruit$Fruit)

[1]"latin1""latin1""latin1"

[1] "latin1" "latin1" "latin1"

fruit$Fruit <- enc2utf8(fruit$Fruit)
Encoding(fruit$Fruit)

[1]"UTF-8""UTF-8""UTF-8"

[1] "UTF-8" "UTF-8" "UTF-8"

这篇关于使用UTF-8编码的read.csv()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆