微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决R:从 2 个 zip 文件夹中读取 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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。