[英]R: Reading a csv from within 2 zip folders
我在一些不幸的情況下工作,需要從 2 個 zip 文件夾中讀取 csv 文件。 我的意思是文件路徑看起來像這樣:
//path/folder1.zip/folder2.zip/wanttoread.csv
我試着模仿這里發現的這個問題的巧妙工作: 從.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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.