数据 table fread with zip file in other directory with spaces in the name
Data table fread with zip file in other directory with spaces in the name
我正在尝试使用命令 fread("unzip -cq file.zip")
读取 zip 文件中的 csv,当文件位于我的工作目录中时该命令运行良好。
但是当我通过指定文件路径而不更改目录来尝试命令时说 fread("unzip -cq C:/Users/My user/file.zip")
我收到一条错误消息 unzip: cannot find either C:/Users/My or C:/Users/My.zip
发生这种情况的原因是我的路径中有空格,但解决方法是什么?
我想到的唯一选择是只切换到每个文件所在的目录并从那里读取它,但这并不理想。
尝试将位置分配给变量并使用粘贴调用 zip 文件,如下所示:
myVar<-"C:/Users/Myuser/"
fread(paste0("unzip -cq ",myVar,"file.zip"))
我为此使用 shQuote
,例如...
fread_zip = function(fp, silent=FALSE){
qfp = shQuote(fp)
patt = "unzip -cq %s"
thecall = sprintf(patt, qfp)
if (!silent) cat("The call:", thecall, sep="\n")
fread(thecall)
}
定义一个模式,然后用 sprintf
替换可以保持内容的可读性和更易于管理。例如,我有一个类似的 .tar.gz
文件包装器(显然需要在步骤之间使用 |
管道解压缩两次)。
如果您的 zip 包含多个 csvs,fread
未设置为读取所有文件(尽管有 an open issue)。我目前针对该案例的解决方法是...
library(magrittr)
fread_zips = function(fp, unzip_dir = file.path(dirname(fp), sprintf("csvtemp_%s", sub(".zip", "", basename(fp)))), silent = FALSE, do_cleanup = TRUE){
# only tested on windows
# fp should be the path to mycsvs.zip
# unzip_dir should be used only for CSVs from inside the zip
dir.create(unzip_dir, showWarnings = FALSE)
# unzip
unzip(fp, overwrite = TRUE, exdir = unzip_dir)
# list files, read separately
# not looking recursively, since csvs should be only one level deep
fns = list.files(unzip_dir)
if (!all(tools::file_ext(fns) == "csv")) stop("fp should contain only CSVs")
res = lapply(fns %>% setNames(file.path(unzip_dir, .), .), fread)
if (do_cleanup) unlink(unzip_dir, recursive = TRUE)
res
}
因此,因为我们没有将命令行调用直接传递给 fread,所以这里不需要 shQuote
。这个函数是我昨天写和用到的,所以可能还有一些疏忽或者bug。
magrittr %>%
管道部分可以写成 setNames(file.path(unzip_dir, fns), fns)
。
我正在尝试使用命令 fread("unzip -cq file.zip")
读取 zip 文件中的 csv,当文件位于我的工作目录中时该命令运行良好。
但是当我通过指定文件路径而不更改目录来尝试命令时说 fread("unzip -cq C:/Users/My user/file.zip")
我收到一条错误消息 unzip: cannot find either C:/Users/My or C:/Users/My.zip
发生这种情况的原因是我的路径中有空格,但解决方法是什么?
我想到的唯一选择是只切换到每个文件所在的目录并从那里读取它,但这并不理想。
尝试将位置分配给变量并使用粘贴调用 zip 文件,如下所示:
myVar<-"C:/Users/Myuser/"
fread(paste0("unzip -cq ",myVar,"file.zip"))
我为此使用 shQuote
,例如...
fread_zip = function(fp, silent=FALSE){
qfp = shQuote(fp)
patt = "unzip -cq %s"
thecall = sprintf(patt, qfp)
if (!silent) cat("The call:", thecall, sep="\n")
fread(thecall)
}
定义一个模式,然后用 sprintf
替换可以保持内容的可读性和更易于管理。例如,我有一个类似的 .tar.gz
文件包装器(显然需要在步骤之间使用 |
管道解压缩两次)。
如果您的 zip 包含多个 csvs,fread
未设置为读取所有文件(尽管有 an open issue)。我目前针对该案例的解决方法是...
library(magrittr)
fread_zips = function(fp, unzip_dir = file.path(dirname(fp), sprintf("csvtemp_%s", sub(".zip", "", basename(fp)))), silent = FALSE, do_cleanup = TRUE){
# only tested on windows
# fp should be the path to mycsvs.zip
# unzip_dir should be used only for CSVs from inside the zip
dir.create(unzip_dir, showWarnings = FALSE)
# unzip
unzip(fp, overwrite = TRUE, exdir = unzip_dir)
# list files, read separately
# not looking recursively, since csvs should be only one level deep
fns = list.files(unzip_dir)
if (!all(tools::file_ext(fns) == "csv")) stop("fp should contain only CSVs")
res = lapply(fns %>% setNames(file.path(unzip_dir, .), .), fread)
if (do_cleanup) unlink(unzip_dir, recursive = TRUE)
res
}
因此,因为我们没有将命令行调用直接传递给 fread,所以这里不需要 shQuote
。这个函数是我昨天写和用到的,所以可能还有一些疏忽或者bug。
magrittr %>%
管道部分可以写成 setNames(file.path(unzip_dir, fns), fns)
。