R - 如何导入带有 block 的巨大 .csv?

标签 r csv import repeat

我一直在尝试导入一个巨大的.csv,其中包含 block 和过滤器。 但我的代码只是读取文件的一部分(2000 万或 4500 万)。

我也已经尝试使用data.table() 但没有成功。

arq_grande <- file("cnpj_dados_cadastrais_pj.csv", "r")
tam_chunk <- 5000
df1 <- read.csv(arq_grande, nrows = 10, header = T, sep = "#", dec = ".")
for(i in 1:ncol(df1)){df1[,i] <- df1[,i] %>% iconv(from = 'UTF-8', to = 'latin1')}
df_filtrado <- df1 %>% filter(codigo_natureza_juridica == c("2143","2330")) %>%  select(cnpj,everything())
write.table(df_filtrado, "/cnpj_dados_cadastrais_pj_filtrado_coop.csv", row.names = F, sep = "#", dec = ".")
names(df1)
nrow <- 1
totalRows <- 0

repeat {
  df <- read.csv(arq_grande, header=FALSE, sep="#", col.names = names(df1), nrows = tam_chunk)
  for(i in 1:ncol(df)){df[,i] <- df[,i] %>% iconv(from = 'UTF-8', to = 'latin1')}
  nRow = nrow(df)
  totalRows <- totalRows + nRow
  cat("Lendo", nrow(df), "linhas, total lido", totalRows, "\n")
  if (nrow(df) == 0)
    break

  df_filtrado <- df %>% filter(codigo_natureza_juridica == c("2143","2330")) %>%  select(cnpj,everything())
  write.table(df_filtrado, "/cnpj_dados_cadastrais_pj_filtrado_coop.csv", append = T, col.names = F, row.names = F, sep = "#", dec = ".")
}
close(arq_grande)

我在这里看到了其他例子,但没有任何效果。抱歉,我对此类数据不熟悉。

我只想读取 .csv 的所有行。

最佳答案

您可以通过 readr::read_csv 使用 skipn_max 参数读取 csv 文件:skip 是开始时要跳过的行数,n_max 是之后要读取的行数。

library("readr")

# Example uses `#` as the separator
file <- "
lineno#X#Y#Z
1#a#b#c
2#d#e#f
3#g#h#i
4#j#k#l
5#m#n#o
6#p#q#r
7#s#t#u
8#v#w#
9#x#y#z
"

# Increase the chunk size appropriately
chunk_size <- 3

# Assumption: There is a header on the first line
# but we don't know what it is.
col_names <- TRUE
line_num <- 1

while (TRUE) {
  chunk <- read_delim(
    file, "#",
    skip = line_num,
    n_max = chunk_size,
    # On the first iteration, col_names is TRUE
    # so the first line "X,Y,Z" is assumed to be the header
    # On any subsequent iteration, col_names is a character vector
    # of the actual column names
    col_names = col_names
  )

  # If the chunk has now rows, then reached end of file
  if (!nrow(chunk)) {
    break
  }

  # Do something with the chunk of data
  print(chunk)

  # Update `col_names` so that it is equal the actual column names
  col_names <- colnames(chunk)

  # Move to the next chunk. Add 1 for the header.
  line_num <- line_num + chunk_size + (line_num == 1)
}
#> # A tibble: 3 x 4
#>   lineno X     Y     Z    
#>    <dbl> <chr> <chr> <chr>
#> 1      1 a     b     c    
#> 2      2 d     e     f    
#> 3      3 g     h     i    
#> # A tibble: 3 x 4
#>   lineno X     Y     Z    
#>    <dbl> <chr> <chr> <chr>
#> 1      4 j     k     l    
#> 2      5 m     n     o    
#> 3      6 p     q     r    
#> # A tibble: 3 x 4
#>   lineno X     Y     Z    
#>    <dbl> <chr> <chr> <chr>
#> 1      7 s     t     u    
#> 2      8 v     w     <NA> 
#> 3      9 x     y     z

reprex package于2019年10月31日创建(v0.3.0)

关于R - 如何导入带有 block 的巨大 .csv?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58601150/

相关文章:

css - 导入 LESS w/out 创建其单独的 CSS 输出文件

与目录同名的 Python 导入类

mysql - 如何将 mysql 表同步到 hive 表? (不支持 sqoop --incremental lastmodified 配置单元导入)

r - 如何从包含R中特殊字符和单词混合的字符串中提取标题

r - 何时使用 approxfun 与 approx

python - 如何将一个 CSV 文件的内容写入另一个文件

python - 在 Mac Yosemite 上的 csvkit python 包中使用 csvsql 实用程序时出错

python - 如何处理 CSV 字典中的 'missing key values' 并通过 Pandas 数据框进行操作?

r - 按名称排列的每一行最近 21 天的事件

r - 如何比较R中的三角函数?