read.csv 警告“带引号的字符串中的 EOF"会阻止完全读取文件 [英] read.csv warning 'EOF within quoted string' prevents complete reading of file

查看:24
本文介绍了read.csv 警告“带引号的字符串中的 EOF"会阻止完全读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a CSV file (24.1 MB) that I cannot fully read into my R session. When I open the file in a spreadsheet program I can see 112,544 rows. When I read it into R with read.csv I only get 56,952 rows and this warning:

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 中:

I can read the whole file into R with readLines:

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

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

But I can't get this back into R as a table (via 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 会话中?

How can I solve or workaround this EOF message (which seems to be more of an error than a warning) to get the entire file into my R session?

我在使用其他读取 CSV 文件的方法时遇到了类似的问题:

I have similar problems with other methods of reading CSV files:

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()

Here's my 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	" "10.2307/30007362	" "10.2307/4254931	" "10.2307/20537934	" ...
##  $ doi          : chr  "Archaeological Inference and Inductive Confirmation	" "Sound and Sense in Cath Almaine	" "Oak Galls Preserved by the Eruption of Mount Vesuvius in A.D. 79_ and Their Probable Use	" "The Arts Four Thousand Years Ago	" ...
##  $ title        : chr  "Bruce D. Smith	" "Tomás Ó Cathasaigh	" "Hiram G. Larew	" "	" ...
##  $ author       : chr  "American Anthropologist	" "Ériu	" "Economic Botany	" "The Illustrated Magazine of Art	" ...
##  $ journaltitle : chr  "79	" "54	" "41	" "1	" ...
##  $ volume       : chr  "3	" "	" "1	" "3	" ...
##  $ issue        : chr  "1977-09-01T00:00:00Z	" "2004-01-01T00:00:00Z	" "1987-01-01T00:00:00Z	" "1853-01-01T00:00:00Z	" ...
##  $ pubdate      : chr  "pp. 598-617	" "pp. 41-47	" "pp. 33-40	" "pp. 171-172	" ...
##  $ pagerange    : chr  "American Anthropological Association	Wiley	" "Royal Irish Academy	" "New York Botanical Garden Press	Springer	" "	" ...
##  $ publisher    : chr  "fla	" "fla	" "fla	" "fla	" ...
##  $ type         : logi  NA NA NA NA NA NA ...
##  $ reviewed.work: logi  NA NA NA NA NA NA ...

我认为是因为这种台词(检查Thorn"和Minus")

I think is because of this kind of lines (check "Thorn" and "Minus")

 readLines("citations.CSV")[82]
[1] "10.2307/3642839,10.2307/3642839	,"Thorn" and "Minus" in Hieroglyphic Luvian Orthography	,H. Craig Melchert	,Anatolian Studies	,38	,	,1988-01-01T00:00:00Z	,pp. 29-42	,British Institute at Ankara	,fla	,	,"

这篇关于read.csv 警告“带引号的字符串中的 EOF"会阻止完全读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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