如何在 R 中读取有序字典数据

How to read ordered dictionary data in R

假设您在 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 中读取相应的 variables/values?

非常感谢。

在这里,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