Home > Blockchain >  How to import a given csv located inside one of the files that are themselves inside a zip?
How to import a given csv located inside one of the files that are themselves inside a zip?

Time:05-20

Let's imagine a zip "a0" that contains 3 files "a01", "a02" and "a03". Using R, how to import a given csv (e.g. "ax.csv") that we don't know in which file is it?

NB : it is assumed that this given csv is unique in the entire zip, but that there may be other csv named differently.

Thanks for help

CodePudding user response:

Use unzip. The first line gets the names of the files in the zip file. We then grep out the file we want. Change the pattern to whatever you are looking for. Finally the second unzip call extracts the csv file and we read it in. No packages are used.

nms <- unzip("test.zip", list = TRUE)$Name
csv_name <- grep("csv$", nms, value = TRUE)
if (length(csv_name) != 1) stop("cannot determine which file to extract")
unzip("test.zip", files = csv_name)
read.csv(csv_name)
  • Related