首页 > 解决方案 > 使用 UTF-8 编码将 csv 文件导入 R 数据帧

问题描述

我正在尝试使用数据框导入 csv 文件:

pc2020 <- read.table("pc2020.csv", sep = ";", header = TRUE) 

这工作正常,但编码是错误的,因此,我把所有的重音字符都搞砸了。

所以,我正在尝试:

pc2020 <- read.table("pc2020.csv", sep = ";", header = TRUE, fileEncoding = "UTF-8") 

返回:

Error in read.table("pc2020.csv", sep = ";", header = TRUE, fileEncoding = "UTF-8") : 
  no lines available in input
In addition: Warning message:
In read.table("pc2020.csv", sep = ";", header = TRUE, fileEncoding = "UTF-8") :
  invalid input found on input connection 'pc2020.csv'

标签: r

解决方案


您可以使用read.csv()具有与 . 相同的属性的函数read.table。除了fileEncoding- 在read.csv()你应该写只是encoding = "UTF-8"

鸭子的答案也很合适。


推荐阅读