0

使用下面的代码将 OIOIUBL 发票从 XML 转换为 RI 中的数据框(示例 XML 文件位于更下方的链接中)。

当迭代 323.859 个文件时,XML 到数据帧的解析需要 64 分钟。从下面的代码如何优化?使用 Python 执行相同的任务大约需要 6 分钟(抱歉无法共享 Python 代码)。

我知道 R 不擅长在内存中移动大量数据。但是 58 分钟的执行时间差异可以归因于这个或我缺乏编程技能吗?

library(xml2)
readinvoice <- function(nextfile) {
    tryCatch(doc <- read_xml(nextfile, options = c("RECOVER", "HUGE")),
            error = function(e) {print(paste("Error in code", basename(nextfile), e$message))})
    art <- xml_name(doc)
    path <- xml_path(doc)
    ID <- gsub('\\.xml', "", basename(nextfile))
    priceTotal <- xml_double(xml_find_all(doc, "./cac:LegalMonetaryTotal/cbc:PayableAmount"))
    tryCatch(df <- data.frame(cbind(art, path, ID, priceTotal)),
            error = function(e) {print(paste("Error in code", bilagsident, e$message))})
    }
xmlfilelist <- list.files("PATH/TO/FILES", full.names = TRUE)
result <- lapply(xmlfilelist, readinvoice) # THIS LINE IS SLOW

示例文件链接:https ://1drv.ms/u/s!Am4CDwbz-dfchPZKhf9RQPSfnKcLeg

4

0 回答 0