【问题标题】:Deal with escaped commas in CSV file?处理 CSV 文件中的转义逗号?
【发布时间】:2017-09-25 20:33:56
【问题描述】:

我正在使用 fread 读取 R 中的文件

test.set = fread("file.csv", header=FALSE, fill=TRUE, blank.lines.skip=TRUE)

我的 csv 包含 6 列。此文件中的一行示例是

"2014-07-03 11:25:56","61073a09d113d3d3a2af6474c92e7d1e2f7e2855","Securenet Systems 电台播放列表更新","你的爱","Fred Hammond & Radical for Christ","50fcfb08424fe1e2c653a87a64ee92d7"

但是,当其中一个单元格内有逗号时,某些行会以特定方式格式化。例如,

"2014-07-03 11:25:59","37780f2e40f3af8752e0d66d50c9363279c55be6","Spotify","\"你好\", 他撒谎了","Red Box","b226ff30a0b83006e5e06582fbb0afd3"

产生一个错误

Expecting 6 cols, but line 5395818 contains text after processing all         
cols. Try again with fill=TRUE. Another reason could be that fread's    
logic in distinguishing one or more fields having embedded sep=','      
and/or (unescaped) '\n' characters within unbalanced unescaped quotes    
has failed. If quote='' doesn't help, please file an issue to figure 
out if the logic could be improved.

如您所见,导致错误的值是“\”你好\“,他撒谎”,我希望 fread 将其读取为“你好,他撒谎”。不过,我不确定如何解决这个问题 - 我已尝试按照建议使用 fill=TRUE 和 quote="" ,但错误仍然不断出现。这可能只是为 fread 找到正确参数的问题;有人知道那些可能是什么吗?

【问题讨论】:

  • read.csv(..., allowEscapes = TRUE)
  • 我知道这适用于 read.csv,但 fread 有类似的东西吗?我正在读取一个多千兆字节的文件,所以我更愿意使用 fread
  • fread 文档中没有任何内容表明存在...

标签: r csv data.table


【解决方案1】:

在来自base Rread.table() 中,这个问题是可以解决的。

使用Import data into R with an unknown number of columns?

freaddata.table 中这是不可能的。

为此记录的问题:https://github.com/Rdatatable/data.table/issues/2669

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-26
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    相关资源
    最近更新 更多