Преобразование строки XML Атрибуты к кадру данных как переменные [дубликат]

Почему бы не использовать новый сканер для каждого чтения? Как показано ниже. При таком подходе вы не столкнетесь с вашей проблемой.

int i = new Scanner(System.in).nextInt();
1
задан vtortola 1 October 2015 в 21:06
поделиться

2 ответа

Используя rvest (который является оберткой вокруг xml2), вы можете сделать это следующим образом:

require(rvest)
require(magrittr)
doc <- xml('<posts>
  <row Id="1" PostTypeId="1" 
AcceptedAnswerId="15" CreationDate="2010-07-19T19:12:12.510" Score="27" 
ViewCount="1647" Body="some text;" OwnerUserId="8" 
LastActivityDate="2010-09-15T21:08:26.077" 
Title="title" AnswerCount="5" CommentCount="1" FavoriteCount="17" />
</posts>')

rows <- doc %>% xml_nodes("row")
data.frame(
  Id = rows %>% xml_attr("id"),
  PostTypeId = rows %>% xml_attr("posttypeid")
)

Результат:

  Id PostTypeId
1  1          1

Если вы берете Комментарии.xml с

data.frame(
  Id = rows %>% xml_attr("id"),
  PostTypeId = rows %>% xml_attr("postid"),
  score = rows %>% xml_attr("score")
)

Вы получаете:

> head(dat)
  Id PostTypeId score
1  1          3     5
2  2          5     0
3  3          9     0
4  4          5    11
5  5          3     1
6  6         14     9
4
ответ дан Aren Cambre 26 August 2018 в 05:22
поделиться

Это действительно большой вариант использования функции xmlEventParse в пакете XML. Это файл размером 200+ Мбайт, и последнее, что вы хотите сделать, - ненужная потеря памяти (разбор XML - это, как известно, интенсивный объем памяти) и время от времени тратится через узлы несколько раз.

Используя xmlEventParse, вы можете также фильтруйте то, что вы делаете или не нуждаетесь, и вы также можете получить индикатор прокрутки там, чтобы вы могли видеть, что происходит.

library(XML)
library(data.table)

# get the # of <rows> quickly; you can approximate if you don't know the
# number or can't run this and then chop down the size of the data.frame
# afterwards
system("grep -c '<row' ~/Desktop/p1.xml")
## 128010

n <- 128010

# pre-populate a data.frame
# you could also just write this data out to a file and read it back in
# which would negate the need to use global variables or pre-allocate
# a data.frame
dat <- data.frame(id=rep(NA_character_, n),
                  post_type_id=rep(NA_character_, n),
                  stringsAsFactors=FALSE)

# setup a progress bar since there are alot of nodes
pb <- txtProgressBar(min=0, max=n, style=3)

# this function will be called for each <row>
# again, you could write to a file/database/whatever vs do this
# data.frame population
idx <- 1
process_row <- function(node, tribs) {
  # update the progress bar
  setTxtProgressBar(pb, idx)
  # get our data (you can filter here)
  dat[idx, "id"] <<- tribs["Id"]
  dat[idx, "post_type_id"] <<- tribs["PostTypeId"]
  # update the index
  idx <<- idx + 1
}

# start the parser
info <- xmlEventParse("Posts.xml", list(row=process_row))

# close up the progress bar
close(pb)

head(dat)
##   id post_type_id
## 1  1            1
## 2  2            1
## 3  3            1
## 4  4            1
## 5  5            2
## 6  6            1
2
ответ дан hrbrmstr 26 August 2018 в 05:22
поделиться
Другие вопросы по тегам:

Похожие вопросы: