gpt4 book ai didi

r - fread 和带有尾部反斜杠的列

转载 作者:行者123 更新时间:2023-12-02 17:23:57 25 4
gpt4 key购买 nike

我在 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"))

如果可能的话。

最佳答案

现已在 GitHub 的 v1.9.3 中修复.

  • fread() now accepts trailing backslash in quoted fields. Thanks to user2970844 for highlighting.
$ 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
>

关于r - fread 和带有尾部反斜杠的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24375832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com