R:从 2 个 zip 文件夹中读取 csv
R: Reading a csv from within 2 zip folders
我在一些不幸的情况下工作,需要从 2 个 zip 文件夹中读取一个 csv 文件。我的意思是文件路径看起来像这样:
//path/folder1.zip/folder2.zip/wanttoread.csv
我试着模仿这里发现的这个问题的圆滑工作:,但到目前为止运气不好。具体来说,当我 运行 我这边有类似的东西时,我收到一条错误消息
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
我在一些不幸的情况下工作,需要从 2 个 zip 文件夹中读取一个 csv 文件。我的意思是文件路径看起来像这样:
//path/folder1.zip/folder2.zip/wanttoread.csv
我试着模仿这里发现的这个问题的圆滑工作:
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