139

我有一个 CSV 文件 (24.1 MB),我无法完全读入我的 R 会话。当我在电子表格程序中打开文件时,我可以看到 112,544 行。当我将它读入 R 时,read.csv我只得到 56,952 行和这个警告:

cit <- read.csv("citations.CSV", row.names = NULL, 
                comment.char = "", header = TRUE, 
                stringsAsFactors = FALSE,  
                colClasses= "character", encoding= "utf-8")

Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  EOF within quoted string

我可以使用以下命令将整个文件读入 R readLines

rl <- readLines(file("citations.CSV", encoding = "utf-8"))
length(rl)
[1] 112545

但我无法将其作为表格返回到 R 中(通过read.csv):

write.table(rl, "rl.txt", quote = FALSE, row.names = FALSE)
rl_in <- read.csv("rl.txt", skip = 1, row.names = NULL)

Warning message:
In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
  EOF within quoted string

如何解决或解决此 EOF 消息(这似乎更像是错误而不是警告)以将整个文件放入我的R会话中?

其他读取 CSV 文件的方法也有类似的问题:

require(sqldf)
cit_sql <- read.csv.sql("citations.CSV", sql = "select * from file")
require(data.table)
cit_dt <- fread("citations.CSV")
require(ff)
cit_ff <- read.csv.ffdf(file="citations.CSV")

这是我的 sessionInfo()

R version 3.0.1 (2013-05-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] tools     tcltk     stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ff_2.2-11             bit_1.1-10            data.table_1.8.8      sqldf_0.4-6.4        
 [5] RSQLite.extfuns_0.0.1 RSQLite_0.11.4        chron_2.3-43          gsubfn_0.6-5         
 [9] proto_0.3-10          DBI_0.2-7   
4

8 回答 8

218

您需要禁用引用。

cit <- read.csv("citations.CSV", quote = "", 
                 row.names = NULL, 
                 stringsAsFactors = FALSE)

str(cit)
## 'data.frame':    112543 obs. of  13 variables:
##  $ row.names    : chr  "10.2307/675394" "10.2307/30007362" "10.2307/4254931" "10.2307/20537934" ...
##  $ id           : chr  "10.2307/675394\t" "10.2307/30007362\t" "10.2307/4254931\t" "10.2307/20537934\t" ...
##  $ doi          : chr  "Archaeological Inference and Inductive Confirmation\t" "Sound and Sense in Cath Almaine\t" "Oak Galls Preserved by the Eruption of Mount Vesuvius in A.D. 79_ and Their Probable Use\t" "The Arts Four Thousand Years Ago\t" ...
##  $ title        : chr  "Bruce D. Smith\t" "Tomás Ó Cathasaigh\t" "Hiram G. Larew\t" "\t" ...
##  $ author       : chr  "American Anthropologist\t" "Ériu\t" "Economic Botany\t" "The Illustrated Magazine of Art\t" ...
##  $ journaltitle : chr  "79\t" "54\t" "41\t" "1\t" ...
##  $ volume       : chr  "3\t" "\t" "1\t" "3\t" ...
##  $ issue        : chr  "1977-09-01T00:00:00Z\t" "2004-01-01T00:00:00Z\t" "1987-01-01T00:00:00Z\t" "1853-01-01T00:00:00Z\t" ...
##  $ pubdate      : chr  "pp. 598-617\t" "pp. 41-47\t" "pp. 33-40\t" "pp. 171-172\t" ...
##  $ pagerange    : chr  "American Anthropological Association\tWiley\t" "Royal Irish Academy\t" "New York Botanical Garden Press\tSpringer\t" "\t" ...
##  $ publisher    : chr  "fla\t" "fla\t" "fla\t" "fla\t" ...
##  $ type         : logi  NA NA NA NA NA NA ...
##  $ reviewed.work: logi  NA NA NA NA NA NA ...

我认为是因为这种线(检查“刺”和“减号”)

 readLines("citations.CSV")[82]
[1] "10.2307/3642839,10.2307/3642839\t,\"Thorn\" and \"Minus\" in Hieroglyphic Luvian Orthography\t,H. Craig Melchert\t,Anatolian Studies\t,38\t,\t,1988-01-01T00:00:00Z\t,pp. 29-42\t,British Institute at Ankara\t,fla\t,\t,"
于 2013-07-01T23:04:57.210 回答
12

我是一个新的 R 用户,我想我会发布这个以防它帮助其他人。我试图从包含几个西班牙字符的文本文件(用逗号分隔)中读取数据,我花了很长时间才弄明白。我知道我需要使用 UTF-8 编码,将标头 arg 设置为 TRUE,并且我需要将 sep 参数设置为“,”,但后来我仍然挂断了。阅读这篇文章后,我尝试将填充参数设置为 TRUE,但随后得到了相同的“引用字符串中的 EOF”,我能够以与上述相同的方式修复它。我成功的 read.table 如下所示:

target <- read.table("target2.txt", fill=TRUE, header=TRUE, quote="", sep=",", encoding="UTF-8")

结果有西班牙语字符和我原来的相同的暗淡,所以我称之为成功!谢谢大家!

于 2015-10-13T21:30:59.483 回答
6

如上所述,在 R 帮助部分中,只需添加以下内容即可完全禁用引用:

    quote = "" 

到 read.csv() 为我工作。

错误“引用字符串中的 EOF”发生在:

    > iproscan.53A.neg     = read.csv("interproscan.53A.neg.n.csv",
    +                        colClasses=c(pb.id      = "character",
    +                                     genLoc     = "character",
    +                                     icode      = "character",
    +                                     length     = "character",
    +                                     proteinDB  = "character",
    +                                     protein.id = "character",
    +                                     prot.desc  = "character",
    +                                     start      = "character",
    +                                     end        = "character",
    +                                     evalue     = "character",
    +                                     tchar      = "character",
    +                                     date       = "character",
    +                                     ipro.id    = "character",
    +                                     prot.name  = "character",
    +                                     go.cat     = "character",
    +                                     reactome.id= "character"),
    +                                     as.is=T,header=F)
    Warning message:
    In scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings,  :
      EOF within quoted string
    > dim(iproscan.53A.neg)
    [1] 69383    16

读入的文件缺少 6,619 行。但是通过禁用引用

    > iproscan.53A.neg     = read.csv("interproscan.53A.neg.n.csv",
    +                        colClasses=c(pb.id      = "character",
    +                                     genLoc     = "character",
    +                                     icode      = "character",
    +                                     length     = "character",
    +                                     proteinDB  = "character",
    +                                     protein.id = "character",
    +                                     prot.desc  = "character",
    +                                     start      = "character",
    +                                     end        = "character",
    +                                     evalue     = "character",
    +                                     tchar      = "character",
    +                                     date       = "character",
    +                                     ipro.id    = "character",
    +                                     prot.name  = "character",
    +                                     go.cat     = "character",
    +                                     reactome.id= "character"),
    +                                     as.is=T,header=F,**quote=""**)    
    > 
    > dim(iproscan.53A.neg)
    [1] 76002    16

工作没有错误,所有行都已成功读入。

于 2015-09-26T21:06:19.673 回答
3

我也遇到了这个问题,并且能够使用以下方法解决类似的 EOF 错误:

read.table("....csv", sep=",", ...)

请注意,分隔符参数是在更通用的read.table().

于 2013-08-01T17:38:20.507 回答
3

我也有类似的问题。但就我而言,问题的原因是某些文本值中存在撇号(即单引号)。这在处理包括法语文本的数据时尤其常见,例如«L'autre jour»。

因此,解决方案是简单地调整quote 参数的默认设置以排除«'» 符号,因此,使用quote = "\"" (即双引号),一切正常。

我希望这可以帮助你们中的一些人。干杯。

于 2018-08-17T21:07:13.830 回答
3

实际上,read.csv()用于读取带有文本内容的文件并不是一个好主意,按设置禁用引号quote=""只是一个临时解决方案,它只适用于单独的引号。还有其他原因会导致警告,例如某些特殊字符。

永久解决方案(使用read.csv()),找出那些特殊字符是什么并使用正则表达式来消除它们是一个想法。

你有没有想过安装包{data.table}fread()用来读取文件。它要快得多,并且不会因为此 EOF 警告而打扰您。请注意,它加载的文件将存储为 data.table 对象,而不是 data.frame 对象。as.data.frame()类 data.table 有很多很好的特性,但无论如何,如果需要,您可以使用它进行转换。

于 2016-12-16T14:08:08.750 回答
2

readr软件包将解决此问题。

install.packages('readr')
library(readr)
readr::read_csv('yourfile.csv')
于 2020-05-15T05:06:23.663 回答
2

I had the similar problem: EOF -warning and only part of data was loading with read.csv(). I tried the quotes="", but it only removed the EOF -warning.

But looking at the first row that was not loading, I found that there was a special character, an arrow → (hexadecimal value 0x1A) in one of the cells. After deleting the arrow I got the data to load normally.

于 2015-07-12T08:30:02.580 回答