【问题标题】:Errors with data frames from json and xml来自 json 和 xml 的数据帧错误
【发布时间】:2020-06-06 01:02:14
【问题描述】:

我需要一个来自 json 或 xml 文件的数据框(数据有两种格式here)。然而,当我尝试在 R 中获取这些数据帧时,我得到了错误。

使用json文件,报错如下文字

parse_con(txt, bigint_as_char) 中的错误:词法错误:UTF8 字符串中的无效字节。 站":"0","name_question":"Óðî÷èñòå çàñ³äàííÿ Âåðõîâíî¿ Ðàä (就在这里)------^

使用xml文件,报错是这样的

[<-.data.frame(*tmp*, i, names(nodes[[i]]), value = c(date_agenda = "27112014", 中的错误:列的下标重复

我使用的命令是

library(jsonlite)
library(XML)

k <- fromJSON("https://data.rada.gov.ua/ogd/zal/ppz/skl8/dict/agendas_8_skl.json", encoding = "UTF-8")
m <- xmlToDataFrame("agendas_8_skl.xml") 

在执行命令之前,我将文件下载到工作目录。

我不明白如何获取数据。请帮忙!

【问题讨论】:

    标签: r json xml utf-8


    【解决方案1】:

    这是一个使用 xml 数据的解决方案。

    详见代码cmets:

    library(xml2)
    library(dplyr)
    
    #read page
    page<-read_xml("https://data.rada.gov.ua/ogd/zal/ppz/skl8/dict/agendas_8_skl.xml")
    
    #obtain a list of parent nodes
    agendas<-xml_find_all(page, "agenda") 
    
    output<-lapply(agendas, function(agenda) {
      #get date
      date<- agenda %>% xml_find_first(".//date_agenda") %>% xml_text() %>% as.Date(format="%d%m%Y")
      #pull question id from attribute
      question_id <-agenda %>% xml_find_all(".//question") %>% xml_attr("id_question")
      #obtain the information from all of the nodes (assumes equal number of each)
      number_questions <-agenda %>%xml_find_all(".//number_question") %>%  xml_text()
      init_questions <-agenda %>%xml_find_all(".//init_question") %>% xml_text()
      name_questions <-agenda %>%xml_find_all(".//name_question") %>% xml_text()
    
      #create a data frame of answer (long format)
      data.frame(date, question_id, number_questions, init_questions, name_questions, stringsAsFactors = FALSE)
    })
    
    #bind into 1 large long formatted data frame
    finalanswer<-bind_rows(output)
    head(finalanswer)
    

    【讨论】:

    • 它给了我一个命令#obtain a list of parent nodes agendas&lt;-xml_nodes(page, "agenda")的错误
    • @YuliiaZhaha,抱歉更新了代码,将 xml_nodes 替换为新的 xml_find_all 函数。 xml_node 在 rvest 包中。
    【解决方案2】:

    此答案基于@user2554330 的答案here

    library(jsonlite)
    library(RCurl) 
    #Incase you have locale different than ukrainian
    Sys.setlocale("LC_CTYPE", "ukrainian")
    k <- fromJSON(getURL("https://data.rada.gov.ua/ogd/zal/ppz/skl8/dict/agendas_8_skl.json", 
                         .encoding = "ISO-8859-5"))
    
    #transfer k into dataframe using tidyr::unnest
    library(dplyr)
    library(tidyr)
    df <- tibble(date_agenda=k[[1]]$date_agenda, question=k[[1]]$question) %>% 
            unnest(question) %>% 
            unnest(reporter_question, keep_empty=TRUE) 
    

    【讨论】:

    • 它给了我一个列表。是否可以从中制作数据框?
    • 我在同一个站点上遇到了问题,但使用了不同的 json 文件。这是我使用的命令 Sys.setlocale("LC_CTYPE", "ukrainian") mps8 &lt;- fromJSON(getURL("https://data.rada.gov.ua/ogd/mps/skl8/mps-data.json", .encoding = "ISO-8859-5")) 但它给了我一个错误。你知道什么可能导致问题吗?
    • @YuliiaZhaha 我已经测试了 URL here,我认为 json 文件已损坏。我尝试readLines 提到here,我得到了数据,但格式很奇怪“可能是由于编码”。我认为你应该开始一个新问题。
    • 在 Linux 机器上使用 readLinesencoding = "UTF-16LE" 效果很好,我添加了一个小 R 脚本,其中包含 RStudio Cloud here 中的完整解决方案,这个项目是公开的,只是你需要一个帐户登录。
    猜你喜欢
    • 2021-12-21
    • 1970-01-01
    • 2020-12-13
    • 2019-04-28
    • 2014-05-27
    • 2018-12-26
    • 2011-11-30
    • 2020-10-12
    • 1970-01-01
    相关资源
    最近更新 更多