【问题标题】:How to read ordered dictionary data in R如何在 R 中读取有序字典数据
【发布时间】:2021-12-28 13:55:56
【问题描述】:

假设您在 A 列的 csv 中有以下 Python“代码”

"[OrderedDict([('YTC', 
OrderedDict([('V', 'B1')])), ('TA', '80'), ('TL', [OrderedDict([('TC', 
  OrderedDict([('V', 'B6'), ('SD', 'AA')])), ('Pe', '10')]), 
OrderedDict([('TC', 
  OrderedDict([('V', 'B5'), ('SD', 'BB')])), ('Pe', '90')]) ])])]"

等

如何读取 R 中的各个变量/值?

非常感谢。

【问题讨论】:

  • 那么这是 OrderedDict 的 OrderedDict 吗?也许看一下“网状”包,首先将其作为 python OrderedDict 读取,然后从那里将值导入 R...

标签: r ordereddictionary


【解决方案1】:

在这里,test.csv 包含您在 A1 中的行,如果您有多行,只需创建一个 for 循环:

library(reticulate)

test<-read.csv("D:/test.csv", header = FALSE)

test1<-strsplit(as.character(test$V1), split="", fixed=TRUE)
test1<-paste0(test1[[1]][2:(length(test1[[1]])-1)], collapse = "")

py_run_string("from collections import OrderedDict ")
py_run_string(paste0("od=", as.character(test1)))

items<-py_run_string("od.items()")

Rlist<-items$od #list of items in R

【讨论】:

  • 非常感谢!这确实回答了我的问题。
猜你喜欢
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
  • 2022-11-16
  • 2020-05-03
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
相关资源
最近更新 更多