我有数百个大型 CSV 文件(每个文件的大小从 10k 行到 100k 行不等),其中一些文件的描述格式不正确,引号内带有引号,因此它们可能看起来像
ID,Description,x
3434,"abc"def",988
2344,"fred",3484
2345,"fr""ed",3485
2346,"joe,fred",3486
我需要能够将 R 中的所有这些行清晰地解析为 CSV。 dput()'ing 它并读取...
txt <- c("ID,Description,x",
"3434,\"abc\"def\",988",
"2344,\"fred\",3484",
"2345,\"fr\"\"ed\",3485",
"2346,\"joe,fred\",3486")
read.csv(text=txt[1:4], colClasses='character')
Error in read.table(file = file, header = header, sep = sep, quote = quote, :
incomplete final line found by readTableHeader on 'text'
如果我们更改引用并且不包含嵌入逗号的最后一行 - 效果很好
read.csv(text=txt[1:4], colClasses='character', quote='')
但是,如果我们更改引用并在最后一行包含嵌入的逗号...
read.csv(text=txt[1:5], colClasses='character', quote='')
Error in scan(file, what, nmax, sep, dec, quote, skip, nlines, na.strings, :
line 1 did not have 4 elements
EDIT x2:应该说不幸的是一些描述中有逗号 - 代码在上面编辑。
最佳答案
更改 quote
设置:
read.csv(text=txt, colClasses='character',quote = "")
ID Description x
1 3434 "abc"def" 988
2 2344 "fred" 3484
3 2345 "fr""ed" 3485
4 2346 "joe" 3486
编辑以处理错误的逗号:
txt <- c("ID,Description,x",
"3434,\"abc\"def\",988",
"2344,\"fred\",3484",
"2345,\"fr\"\"ed\",3485",
"2346,\"joe,fred\",3486")
txt2 <- readLines(textConnection(txt))
txt2 <- strsplit(txt2,",")
txt2 <- lapply(txt2,function(x) c(x[1],paste(x[2:(length(x)-1)],collapse=","),x[length(x)]) )
m <- do.call("rbind",txt2)
df <- as.data.frame(m,stringsAsFactors = FALSE)
names(df) <- df[1,]
df <- df[-1,]
# ID Description x
# 2 3434 "abc"def" 988
# 3 2344 "fred" 3484
# 4 2345 "fr""ed" 3485
# 5 2346 "joe,fred" 3486
不知道,如果这对您的用例来说足够有效。
关于在 R 中读取格式错误的 csv - 不匹配的引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15830470/