2015-09-07 136 views
1

我嘗試將大文件讀入r。在嘗試閱讀時發生此錯誤。即使當我跳過第一條800607線時,它也不會消失。我也嘗試用命令刪除終端中的行。跳過在fread中產生錯誤的行/行R

sed '800608d' filename.csv 

它沒有解決我的問題。如果你能幫助我,我將不勝感激。

原來的錯誤,我自R得到的是:

> data<-fread("filename.csv") 
Read 2.0% of 34143409 rows 
Error in fread("filename.csv") : 
Field 16 on line 800607 starts with quote (") but then has a problem. It can contain balanced unescaped quoted subregions but if it does it can't contain embedded \n as well. Check for unbalanced unescaped quotes: """The attorney for Martin's family, Benjamin Crump, says the evidence is ""irrelevant\"""" """".","NULL","NULL","NULL","NULL","NULL","NULL","NULL","Negative" 
In addition: Warning message: 
In fread("filename.csv") : 
Starting data input on line 8 and discarded previous non-empty line: done 
+0

這是一個非常棘手的問題。問題在於你的文件中有一列使用與文件結構相同的特殊字符(「用於引用」,「作爲分隔符等),所以它完全混淆了文件格式。理想的方法是更改文件格式,如果您有權訪問源文件,例如,將默認引號字符設置爲'而不是「。否則,提供實際的文件將會很有幫助,這樣我們也可以看看它 –

+0

不幸的是,我不允許訪問,並且更改文件格式需要很長時間。 – Carlo

回答

1

我目前在解決這類問題我自己的中間。我不確定這是否適用於所有情況 - 更不用說我正在使用的所有文件。但現在我似乎得到了一些成績有:

skip.list <- c() 

for (i in 1:length(dir(input.dir))){ # i=3 
    file <- dir(input.dir)[i] 
    ingested.file <- NULL 
    ingested.file <- try(fread(paste0(input.dir,file), header=T, stringsAsFactors=F)) 
    if (class(ingested.file)=="try-error") { 
    error.line <-as.integer(sub(" .*","",sub(".*but line ","",as.character(ingested.file)))) 
    app.reviews.input <- try(fread(paste0(input.dir,file), header=T, stringsAsFactors=F,skip=error.line)) 
    if (class(ingested.file)=="try-error") { 
     skip.list_by.downloads <- c(skip.list_by.downloads, file) 
     next 
    } 
    } 
} 

我目前有大約750每1000行的文件工作 - 約50的有同樣的問題。然而,用這種方法,我可以閱讀其中的30個;其餘20個似乎在多行中有錯誤,但我無法指定多個跳過值。

如果可以指定更多的跳躍,那麼你可以嘗試一個while語句。即

while (class(ingested.file)=="try-error") ...然後根據需要自動更新error.list。

我希望這有助於!

+0

對不起,最後一個附錄:您可能需要更改error.line值,具體取決於您獲得的錯誤。 – mjfred

+0

除了將此附錄作爲註釋添加外,最好將其編輯到答案中。不保證評論得到保留,有些人不會閱讀。謝謝! –