简体   繁体   中英

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. 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). 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?

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.

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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