代码之家  ›  专栏  ›  技术社区  ›  Jeromy Anglim

使用R下载压缩的数据文件,提取和导入数据

  •  107
  • Jeromy Anglim  · 技术社区  · 15 年前

    “很多在线CSV都是压缩的。有没有一种方法可以使用R下载、解压归档文件并将数据加载到data.frame#“状态”

    我今天也试着这么做,但最后只是手动下载zip文件。

    我试过这样的方法:

    fileName <- "http://www.newcl.org/data/zipfiles/a1.zip"
    con1 <- unz(fileName, filename="a1.dat", open = "r")
    

    有什么想法吗?

    7 回复  |  直到 4 年前
        1
  •  188
  •   Dirk is no longer here    15 年前

    Zip存档实际上更像是一个包含内容元数据等的“文件系统” help(unzip)

    1. 创建临时文件。文件名(例如 tempfile() )
    2. 使用 download.file() 把文件放到临时文件里。文件
    3. 使用 unz()
    4. 通过删除临时文件 unlink()

    temp <- tempfile()
    download.file("http://www.newcl.org/data/zipfiles/a1.zip",temp)
    data <- read.table(unz(temp, "a1.dat"))
    unlink(temp)
    

    .z .gz )或bzip2ed( .bz2 只是文件 以及那些你可以直接从连接中读取的内容。所以让数据提供者使用它:)

        2
  •  28
  •   Yorgos    15 年前

    temp <- tempfile()
    download.file("http://www.newcl.org/data/zipfiles/a1.zip",temp)
    con <- unz(temp, "a1.dat")
    data <- matrix(scan(con),ncol=4,byrow=TRUE)
    unlink(temp)
    
        3
  •  18
  •   sebastian-c    9 年前

    我用的是在 http://cran.r-project.org/web/packages/downloader/index.html . 容易多了。

    download(url, dest="dataset.zip", mode="wb") 
    unzip ("dataset.zip", exdir = "./")
    
        4
  •  10
  •   dnlbrky    9 年前

    对于Mac(我假设是Linux)。。。

    如果zip存档包含单个文件,则可以使用bash命令 funzip fread data.table 包裹:

    library(data.table)
    dt <- fread("curl http://www.newcl.org/data/zipfiles/a1.zip | funzip")
    

    在存档包含多个文件的情况下,可以使用 tar 而是将特定文件提取到stdout:

    dt <- fread("curl http://www.newcl.org/data/zipfiles/a1.zip | tar -xf- --to-stdout *a1.dat")
    
        5
  •  10
  •   ColinTea    8 年前

    下面是一个适用于不能用 read.table 功能。本例读取一个.xls文件。

    url <-"https://www1.toronto.ca/City_Of_Toronto/Information_Technology/Open_Data/Data_Sets/Assets/Files/fire_stns.zip"
    
    temp <- tempfile()
    temp2 <- tempfile()
    
    download.file(url, temp)
    unzip(zipfile = temp, exdir = temp2)
    data <- read_xls(file.path(temp2, "fire station x_y.xls"))
    
    unlink(c(temp, temp2))
    
        6
  •  5
  •   C8H10N4O2    8 年前

    为了使用data.table实现这一点,我发现下面的方法是有效的。不幸的是,链接不再工作了,所以我使用了另一个数据集的链接。

    library(data.table)
    temp <- tempfile()
    download.file("https://www.bls.gov/tus/special.requests/atusact_0315.zip", temp)
    timeUse <- fread(unzip(temp, files = "atusact_0315.dat"))
    rm(temp)
    

    我知道这在一行中是可能的,因为您可以将bash脚本传递给 fread 弗雷德 .

        7
  •  4
  •   Peter Badida I'm Geeker    9 年前

    试试这个代码。对我来说很有用:

    unzip(zipfile="<directory and filename>",
          exdir="<directory where the content will be extracted>")
    

    unzip(zipfile="./data/Data.zip",exdir="./data")
    
        8
  •  0
  •   Coder-256    5 年前

    我发现下面这些对我有用。这些步骤来自BTD的YouTube视频, Managing Zipfile's in R

    zip.url <- "url_address.zip"
    
    dir <- getwd()
    
    zip.file <- "file_name.zip"
    
    zip.combine <- as.character(paste(dir, zip.file, sep = "/"))
    
    download.file(zip.url, destfile = zip.combine)
    
    unzip(zip.file)
    
        9
  •  0
  •   camnesia    4 年前

    rio() 非常适合这种情况—它使用文件名的文件扩展名来确定它是什么类型的文件,因此可以处理各种各样的文件类型。我也用过 unzip() 列出zip文件中的文件名,因此不必手动指定文件名。

    library(rio)
    
    # create a temporary directory
    td <- tempdir()
    
    # create a temporary file
    tf <- tempfile(tmpdir=td, fileext=".zip")
    
    # download file from internet into temporary location
    download.file("http://download.companieshouse.gov.uk/BasicCompanyData-part1.zip", tf)
    
    # list zip archive
    file_names <- unzip(tf, list=TRUE)
    
    # extract files from zip file
    unzip(tf, exdir=td, overwrite=TRUE)
    
    # use when zip file has only one file
    data <- import(file.path(td, file_names$Name[1]))
    
    # use when zip file has multiple files
    data_multiple <- lapply(file_names$Name, function(x) import(file.path(td, x)))
    
    # delete the files and directories
    unlink(td)
    
    推荐文章