[英]Running a loop on multiple xml files in one folder in R
I am trying to run this script as a loop function as I have over 200 files in a folder and I am trying to produce one CSV file at the end listing all of the data that I need to extract.我正在尝试将此脚本作为循环 function 运行,因为我在一个文件夹中有 200 多个文件,并且我试图在最后生成一个 CSV 文件,其中列出了我需要提取的所有数据。
I have tried various ways of running this in a loop eg In R, how to extracting two values from XML file, looping over 5603 files and write to table我尝试了各种在循环中运行它的方法,例如在 R 中,如何从 XML 文件中提取两个值,循环 5603 个文件并写入表
Whenever I do try these different options I get errors like:每当我尝试这些不同的选项时,我都会收到如下错误:
Error: XML content does not seem to be XML or permission denied.错误:XML 内容似乎不是 XML 或权限被拒绝。
However, when I run the code selecting just one file it runs fine.但是,当我只选择一个文件运行代码时,它运行良好。 These errors only seems to occur when I try convert it into a loop function for multiple files in a single folder.
这些错误似乎仅在我尝试将其转换为单个文件夹中多个文件的循环 function 时发生。
Here is the original code used for a single file:这是用于单个文件的原始代码:
doc<-xmlParse("//file/path/32460004.xml")
xmldf <- xmlToDataFrame(nodes = getNodeSet(doc, "//BatRecord"))
df1 <- data.frame(xmldf)
df1 <- separate(df1, xmldf.DateTime, into = c("Date", "Time"), sep = " ")
df1$Lat <- substr(xmldf$GPS,4,12)
df1$Long <- substr(xmldf$GPS,13,25)
df_final <- data.frame(df1$xmldf.Filename, df1$Date, df1$Time, df1$xmldf.Duration, df1$xmldf.Temperature, df1$Lat, df1$Long)
colnames(df_final) <- c("Filename", "Date", "Time", "Call Duration", "Temperature", "Lat", "Long")
write.csv(df_final, "//file/path/test_file.csv")
Here is a link to some example files:以下是一些示例文件的链接:
https://drive.google.com/drive/folders/1ZvmOEWhzlWHRl2GxZrbYY9y7YSZ5j9Fj?usp=sharing https://drive.google.com/drive/folders/1ZvmOEWhzlWHRl2GxZrbYY9y7YSZ5j9Fj?usp=sharing
Any help is appreciated.任何帮助表示赞赏。
Here is my version details:这是我的版本详细信息:
platform x86_64-w64-mingw32
arch x86_64
os mingw32
system x86_64, mingw32
status
major 3
minor 6.3
year 2020
month 02
day 29
svn rev 77875
language R
version.string R version 3.6.3 (2020-02-29)
nickname Holding the Windsock
This should work using tidyverse
and xml2
.这应该使用
tidyverse
和xml2
工作。
require(tidyverse)
require(xml2)
### Put all your xml files in a vector
my_files <- list.files("path/to/your/xml/files", full.names = TRUE)
### Read function to transform them to tibble (similar to data.frame)
read_my_xml <- function(x, path = "//BatRecord") {
tmp <- read_xml(x) # read the xml file
tmp <- tmp %>%
xml_find_first(path) %>% # select the //BatRecord node
xml_children # select all children of that node
# this extracts the text of all children
# aka the text between the > TEXT </ Tags
out <- tmp %>% xml_text
# Takes the names of the tags <NAME> ... </NAME>
names(out) <- tmp %>% xml_name
# Turns out to tibble - see https://stackoverflow.com/q/40036207/3301344
bind_rows(out)
}
### Read the files as data
dat <- map_df(my_files, read_my_xml) # map_df is similar to a loop + binding it to one tibble
### To the transformation
dat %>%
separate(DateTime, into = c("Date", "Time"), sep = " ") %>%
mutate(Lat = substr(GPS,4,12), Long = substr(GPS,13,25)) %>%
write_csv("wherever/you/want/file.txt")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.