read.csv警告'引用string中的EOF'阻止完整读取文件

我有一个CSV文件(24.1 MB) ,我不能完全读入我的R会话。 当我在电子表格程序中打开文件时,可以看到112,544行。 当我用read.csv把它读入R时,我只能得到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 

我可以用readLines将整个文件读入R:

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

但是我不能把它作为一个表(通过read.csv )返回到R:

 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 

您需要禁用引号。

 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 AD 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," 

我是一个新的R用户,并认为我会张贴这个以防万一有帮助的人。 我试图从一个文本文件中读取数据(用逗号分隔),其中包含一些西class牙文字符,我花了我的时间才弄明白。 我知道我需要使用UTF-8编码,将头部arg设置为TRUE,并且我需要将sep arguemnt设置为“,”,但是我仍然有挂起。 在阅读这篇文章后,我尝试将填充参数设置为TRUE,但是之后得到了与上面相同的方式修复的“引用string中的EOF”。 我成功的read.table如下所示:

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

结果有西class牙语言字符和我原本一样的昏暗,所以我称之为成功! 谢谢大家!

在R的帮助部分,正如上面所指出的,只需简单地添加引用即可:

  quote = "" 

到read.csv()为我工作。

错误“引用string中的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 

没有错误的工作,所有的行都被成功地读入。

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

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

请注意,separator参数是在更一般的read.table()定义的。

我有类似的问题:EOF警告和只有部分数据加载与read.csv()。 我尝试了quotes =“”,但它只是删除了EOF警告。

但是看看没有加载的第一行,我发现其中一个单元格中有一个特殊字符,箭头→(hex值0x1A)。 删除箭头后,我得到的数据正常加载。

实际上,使用read.csv()读取文本内容并不是一个好主意,因为set quot =“”只是一个临时的解决scheme,所以只能使用单独的引号。 还有其他原因会导致警告,例如一些特殊字符。

所以在这些特殊的情况下,永久的解决办法是检查你的文件,找出那些特殊字符是什么,用正则expression式来消除它们。

你有没有想过安装包{data.table}并使用fread()来读取文件。 这是更快,不会打扰你的EOF警告。 注意你读取的文件不是类data.frame,data.table
有很多好的function,但是如果需要的话,你可以使用as.data.frame()来改变它。