R:从 2 个 zip 文件夹中读取 csv

标签 r zip read.csv

我在一些不幸的情况下工作,需要从 2 个 zip 文件夹中读取 csv 文件。我的意思是文件路径看起来像这样:

//path/folder1.zip/folder2.zip/wanttoread.csv

我尝试模仿这里发现的这个问题的巧妙工作:Extract certain files from .zip ,但到目前为止还没有运气。具体来说,当我运行类似的东西时,我收到一条错误消息:

Error in fread(x, sep = ",", header = TRUE, stringsAsFactors = FALSE) : 
embedded nul in string:

接下来是一堆编码的废话。

关于如何处理这个问题有什么想法吗?提前致谢!

最佳答案

这是使用 tempdir() 的方法:

temp<-tempdir(check = TRUE) #Create temporary directory to extract into

unzip("folder1.zip",exdir = temp) #Unzip outer archive to temp directory

unzip(file.path(temp,"folder2.zip"), #Use file.path to generate the path to the inner archive
      exdir = file.path(temp,"temp2")) #Extract to a subfolder inside temp
                                       #This covers the case when the outer archive might also have a file named wanttoread.csv

list.files(file.path(temp,"temp2")) #We can see the .csv file is now there
#[1] "wanttoread.csv"

read.csv(file.path(temp,"temp2","wanttoread.csv")) #Read it in
#   Var1         Var2
#1 Hello obewanjacobi

关于R:从 2 个 zip 文件夹中读取 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67012098/

相关文章:

r - 来自 H2O Mojo/Pojo 的分类 TreeMap

java - JUnit:将 zip 文件从 res 复制到 Android 文件系统

R:读取 .csv 将所有 ""(空格)变为 NA

r - colClasses 日期和时间 read.csv

使用 read.csv() 读取逗号分隔的字符串

r - lapply 与嵌套列表

r - R glm.nb预测三个变量返回错误

android - ZipResourceFile 无法解析为类型

r - 缩进使用 R-ReporteRs 制作的 Flextable

python - 压缩到python中的特定目录