解析存储在一个 zip 文件中的多个 XBRL 文件
Parse multiple XBRL files stored in a zip file
我从一个网站下载了多个 zip 文件。每个 zip 文件包含多个 html
和 xml
扩展文件(每个约 100K)。
可以手动提取文件然后解析它们。但是,我希望能够在 R
(如果可能)
内完成此操作
示例文件(抱歉有点大)使用代码来自
previous question
- 下载一个 zip 文件
library(XML)
pth <- "http://download.companieshouse.gov.uk/en_monthlyaccountsdata.html"
doc <- htmlParse(pth)
myfiles <- doc["//a[contains(text(),'Accounts_Monthly_Data')]", fun = xmlAttrs][[1]]
fileURLS <- file.path("http://download.companieshouse.gov.uk", myfiles) [[1]]
dir.create("temp", "hmrcCache")
download.file(fileURLS, destfile = file.path("temp", myfiles))
我可以使用
XBRL package
如果我手动提取它们。
这可以按如下方式完成
library(XBRL)
inst <- file.path("temp", "Prod224_0004_00000121_20130630.html")
out <- xbrlDoAll(inst, cache.dir="temp/hmrcCache", prefix.out=NULL, verbose=T)
我正在为如何从 zip 文件夹中提取这些文件并解析每个文件而苦苦挣扎,比如说,使用 R 在循环中,而不是手动提取它们。
我试着开始,但不知道如何从这里取得进展。感谢您的任何建议。
# Get names of files
lst <- unzip(file.path("temp", myfiles), list=TRUE)
dim(lst) # 118626
# unzip and extract first file
nms <- lst$Name[1] # Prod224_0004_00000121_20130630.html
lst2 <- unz(file.path("temp", myfiles), filename=nms)
我正在使用 Windows 8.1
R 版本 3.1.2 (2014-10-31)
平台:x86_64-w64-mingw32/x64(64 位)
根据评论中 Karsten 的建议,我将文件解压缩到一个临时目录,然后解析每个文件。我使用 snow
包来加快速度。
# Parse one zip file to start
fls <- list.files(temp)[[1]]
# Unzip
tmp <- tempdir()
lst <- unzip(file.path(temp, fls), exdir=tmp)
# Only parse first 10 records
inst <- lst[1:10]
# Start to parse - in parallel
cl <- makeCluster(parallel::detectCores())
clusterCall(cl, function() library(XBRL))
# Start
st <- Sys.time()
out <- parLapply(cl, inst, function(i)
xbrlDoAll(i,
cache.dir="temp/hmrcCache",
prefix.out=NULL, verbose=T) )
stopCluster(cl)
Sys.time() - st
我从一个网站下载了多个 zip 文件。每个 zip 文件包含多个 html
和 xml
扩展文件(每个约 100K)。
可以手动提取文件然后解析它们。但是,我希望能够在 R
(如果可能)
示例文件(抱歉有点大)使用代码来自 previous question - 下载一个 zip 文件
library(XML)
pth <- "http://download.companieshouse.gov.uk/en_monthlyaccountsdata.html"
doc <- htmlParse(pth)
myfiles <- doc["//a[contains(text(),'Accounts_Monthly_Data')]", fun = xmlAttrs][[1]]
fileURLS <- file.path("http://download.companieshouse.gov.uk", myfiles) [[1]]
dir.create("temp", "hmrcCache")
download.file(fileURLS, destfile = file.path("temp", myfiles))
我可以使用
XBRL package
如果我手动提取它们。
这可以按如下方式完成
library(XBRL)
inst <- file.path("temp", "Prod224_0004_00000121_20130630.html")
out <- xbrlDoAll(inst, cache.dir="temp/hmrcCache", prefix.out=NULL, verbose=T)
我正在为如何从 zip 文件夹中提取这些文件并解析每个文件而苦苦挣扎,比如说,使用 R 在循环中,而不是手动提取它们。 我试着开始,但不知道如何从这里取得进展。感谢您的任何建议。
# Get names of files
lst <- unzip(file.path("temp", myfiles), list=TRUE)
dim(lst) # 118626
# unzip and extract first file
nms <- lst$Name[1] # Prod224_0004_00000121_20130630.html
lst2 <- unz(file.path("temp", myfiles), filename=nms)
我正在使用 Windows 8.1
R 版本 3.1.2 (2014-10-31)
平台:x86_64-w64-mingw32/x64(64 位)
根据评论中 Karsten 的建议,我将文件解压缩到一个临时目录,然后解析每个文件。我使用 snow
包来加快速度。
# Parse one zip file to start
fls <- list.files(temp)[[1]]
# Unzip
tmp <- tempdir()
lst <- unzip(file.path(temp, fls), exdir=tmp)
# Only parse first 10 records
inst <- lst[1:10]
# Start to parse - in parallel
cl <- makeCluster(parallel::detectCores())
clusterCall(cl, function() library(XBRL))
# Start
st <- Sys.time()
out <- parLapply(cl, inst, function(i)
xbrlDoAll(i,
cache.dir="temp/hmrcCache",
prefix.out=NULL, verbose=T) )
stopCluster(cl)
Sys.time() - st