简体   繁体   English

从具有多个文件夹的R压缩的临时文件中读取csv文件

[英]read csv file from zipped temp file with multiple folders in R

I am trying to read a csv file that is contained in a file I extracted from the web. 我正在尝试读取从网络提取的文件中包含的csv文件。 The problem is the zipped file has multiple cascading folders. 问题是压缩文件具有多个级联文件夹。 I have to do that for several different units, so I am performing a loop. 我必须为几个不同的单元执行此操作,因此我正在执行一个循环。 There is no problem with the loop, the file name is correct and I get to download the file. 循环没有问题,文件名正确,我可以下载文件。 However I get an error message (and I think is because R cannot find the exact file I am asking it to find). 但是我收到一条错误消息(我认为是因为R无法找到我要它查找的确切文件)。 The error is: 错误是:

Error in open.connection(file, "rt") : cannot open the connection
In addition: Warning message:
In open.connection(file, "rt") :
  cannot locate file 'XXXX.csv' in zip file 'c:\yyy\temp\bla\'


download.file(paste("http://web.com_",units[i],"_",places[j],".zip",
                     sep=""),
                     temp,
                     cacheOK = F )
data <- read.csv2(unz(temp,
                   paste("name_",units[i],"_",places[j],".csv",
                   sep="")),
                   header=F,
                   skip=1)
unlink(temp)
fili<-rbind(X,
            data)

}

How do I make R find the file I want? 如何让R查找所需的文件?

You have the right approach but (as the warning tells you) the wrong filename. 您使用正确的方法,但是(如警告所言)文件名错误。

It's worth double checking that the zip file does exist before you start trying to read its contents. 在开始尝试读取zip文件之前,应仔细检查该zip文件是否确实存在。

if(file.exists(temp))
{
  read.csv2(unz(...))
} else
{
  stop("ZIP file has not been downloaded to the place you expected.")
}

It's also a good idea to a browse around inside the downloaded file (you may wish to unzip it first) to make sure that you are looking in the right place for the CSV contents. 在下载的文件内部进行浏览也是一个好主意(您可能希望先将其解压缩),以确保在正确的位置查找CSV内容。

It looks like the file, you're going to read, is located in directory. 您将要阅读的文件似乎位于目录中。 In this case your reading should be changed as follows: 在这种情况下,您的阅读应按以下方式更改:

data <- read.csv2(unz(temp,
                   paste("**dirname**/name_",units[i],"_",places[j],".csv",
                   sep="")),
                   header=F,
                   skip=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM