在R中自动读取zip文件 [英] Automate zip file reading in R

查看:100
本文介绍了在R中自动读取zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要自动化R来读取zip文件中的csv数据文件。

I need to automate R to read a csv datafile that's into a zip file.

例如,我将输入:

read.zip(file = "myfile.zip")

在内部,要做的是:


  • 解压缩 myfile.zip 到临时文件夹

  • 使用 read.csv
  • $ b $读取其中包含的唯一文件b
  • Unzip myfile.zip to a temporary folder
  • Read the only file contained on it using read.csv

如果zip文件中有多个文件,则会引发错误。

If there is more than one file into the zip file, an error is thrown.

我的问题是获取包含在zip文件中的文件的名称,按顺序提供 read.csv 命令。有人知道怎么做吗?

My problem is to get the name of the file contained into the zip file, in orded to provide it do the read.csv command. Does anyone know how to do it?

更新

这是我的功能基于@Paul的答案:

Here's the function I wrote based on @Paul answer:

read.zip <- function(zipfile, row.names=NULL, dec=".") {
    # Create a name for the dir where we'll unzip
    zipdir <- tempfile()
    # Create the dir using that name
    dir.create(zipdir)
    # Unzip the file into the dir
    unzip(zipfile, exdir=zipdir)
    # Get the files into the dir
    files <- list.files(zipdir)
    # Throw an error if there's more than one
    if(length(files)>1) stop("More than one data file inside zip")
    # Get the full name of the file
    file <- paste(zipdir, files[1], sep="/")
    # Read the file
    read.csv(file, row.names, dec)
}

因为我将在 tempdir中处理更多文件(),我在其中创建了一个新目录,所以我不会感到困惑h文件。我希望它可能有用!

Since I'll be working with more files inside the tempdir(), I created a new dir inside it, so I don't get confused with the files. I hope it may be useful!

推荐答案

您可以使用 unzip 来解压缩文件。我只是提到这一点,因为从您的问题中尚不清楚您是否知道这一点。关于读取文件。将文件提取到临时目录(?tempdir )后,只需使用 list.files 查找可以在这里转储到临时目录中。在您的情况下,这只是一个文件,即您需要的文件。这样,使用 read.csv 进行读取非常简单:

You can use unzip to unzip the file. I just mention this as it is not clear from your question whether you knew that. In regard to reading the file. Once your extracted the file to a temporary dir (?tempdir), just use list.files to find the files that where dumped into the temporary directory. In your case this is just one file, the file you need. Reading it using read.csv is then quite straightforward:

l = list.files(temp_path)
read.csv(l[1])

假设 tempdir 位置存储在 temp_path 中。

这篇关于在R中自动读取zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆