python 逐行读取csv_在R中如何逐行读取CSV文件并将内容识别为正确的数据类型?...
根據DWin的評論,您可以嘗試這樣的事情:
read.clump <- function(file, lines, clump){
if(clump > 1){
header <- read.csv(file, nrows=1, header=FALSE)
p = read.csv(file, skip = lines*(clump-1),
#p = read.csv(file, skip = (lines*(clump-1))+1 if not a textConnection
nrows = lines, header=FALSE)
names(p) = header
} else {
p = read.csv(file, skip = lines*(clump-1), nrows = lines)
}
return(p)
}您也應該在函數中添加一些錯誤處理/檢查。
然后用
x = "letter1, letter2
a, b
c, d
e, f
g, h
i, j
k, l"
>read.clump(textConnection(x), lines = 2, clump = 1)
letter1 letter2
1 a b
2 c d
> read.clump(textConnection(x), lines = 2, clump = 2)
letter1 letter2
1 e f
2 g h
> read.clump(textConnection(x), lines = 3, clump = 1)
letter1 letter2
1 a b
2 c d
3 e f
> read.clump(textConnection(x), lines = 3, clump = 2)
letter1 letter2
1 g h
2 i j
3 k l現在你只需要*應用于團塊
總結
以上是生活随笔為你收集整理的python 逐行读取csv_在R中如何逐行读取CSV文件并将内容识别为正确的数据类型?...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ajax解析JSON文件
- 下一篇: 线程,进程,并发,并行