【问题标题】:fread and column with a trailing backslashfread 和 column 带有尾随反斜杠
【发布时间】:2014-06-23 22:52:00
【问题描述】:

我在 fread() 使用“\”作为目录分隔符读取一列目录路径时遇到问题。问题是尾随目录分隔符在 fread() 中引发错误。

对于以下示例 csv 文件,

file,size
"windows\user",123

fread() 和 read.csv() 都同意并且都将 \ 转换为 \\

> fread("example.csv")
            file size
1: windows\\user  123

但是,对于以下示例,fread() 会出错,而 read.csv() 很好。

file,size
"windows\user\",123

read.csv() 给出

> read.csv("example.csv")
             file size
1 windows\\user\\  123

虽然 fread() 错误看起来像这样

> fread("example.csv",verbose=TRUE)
Input contains no \n. Taking this to be a filename to open
File opened, filesize is 0.000 GB
File is opened and mapped ok
Detected eol as \r\n (CRLF) in that order, the Windows standard.
Using line 2 to detect sep (the last non blank line in the first 'autostart') ... sep=','
Found 2 columns
First row with 2 fields occurs on line 1 (either column names or first row of data)
All the fields on line 1 are character fields. Treating as the column names.
Count of eol after first data row: 2
Subtracted 1 for last eol and any trailing empty lines, leaving 1 data rows
Error in fread("example.csv", verbose = TRUE) : 
' ends field 1 on line 1 when detecting types: "windows\user\",123

我真的很想避免这样做

DT = data.table(read.csv("example.csv"))

如果可能的话。

【问题讨论】:

  • 碰巧的是,我刚刚在引用字段中与\n 一起修复了这个问题。准备好从GitHub尝试时添加答案。
  • 它确实让人想知道什么是“正确的”修复,因为它是由于scan 的有据可查的行为,并且与这个提问者的说法相反,这个例子不适合阅读。 .csv()。 'file,size "windows\user\",123` 抛出错误。
  • @BondedDust read.csv 似乎对我来说读得很好,同意提问者的观点。我查看了?scan - 你是什么意思?
  • scan 将 '\user' 解释为 ctrl-u 后跟 'ser'。 read.csv(text="windows\user\",123", sep=",") 返回:错误:在“windows\u”开头的字符串中使用了没有十六进制数字的“\u”。Mac 10.8.5,R 3.1.0
  • @BondedDust 那不是read.csv,那是解析器。尝试在控制台自己输入"windows\user\",123",你会得到同样的错误。要解析,您需要将 \ 加倍。从询问者显示的内容读取文件时,read.csv(filename) 有效。

标签: r data.table


【解决方案1】:

现已在 v1.9.3 中修复 GitHub

  • fread() 现在接受带引号的字段中的尾随反斜杠。感谢 user2970844 的突出显示。
$ cat example.csv
file,size
"windows\user\",123

> require(data.table)
> fread("example.csv")
              file size
1: windows\\user\\  123
> read.csv("example.csv")
             file size
1 windows\\user\\  123
> 

【讨论】:

  • 这是我在过去几天看到的两个修复。看到事情这么快完成真是太好了。谢谢。
  • 我刚刚编译了 github 版本,它似乎适用于我的实际数据,但对于我的玩具示例 fread 给了我一个空的 data.table。我确定这只是一个极端情况,但请尝试fread("file,size\n\"windows\\user\\\",123\n")
  • @user2970844 我将您的示例包含在测试套件(测试 1337 和 1338)中,所以应该没问题。您能否运行test.data.table() 并告诉我最后一行。我也粘贴了该命令并为我工作。我在"All 1341.3 tests ... completed ok"
  • 测试 1010.1, 1011 运行没有错误,但检查 x 等于 y 失败:
  • 对不起...我从 github 重新安装并注意到有关未复制 dll 的警告。我认为这是因为我已经打开了一个现有的 R 会话。现在一切都按预期工作,我只收到您之前从 winbuilder 中提到的两个错误(1010.1、1011)。非常感谢@MattDowle。
猜你喜欢
  • 2020-05-20
  • 2020-03-14
  • 2021-05-06
  • 2023-01-30
  • 1970-01-01
  • 2013-12-18
  • 1970-01-01
  • 2011-02-11
  • 1970-01-01
相关资源
最近更新 更多