How to read ordered dictionary data in R

Viewed 59

Assume that you have in a csv in column A the following Python "code"

"[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')]) ])])]"

etc

How can I read the respective variables/values in R?

Many thanks.

1 Answers

Here it is, test.csv contain your line in A1, just create a for loop if you have multiple lines:

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

Related