David Z
David Z

Reputation: 7051

How to read python output into R

My customer has a Python output in abc.txt such like:

[[1.1,2.2,3.3,
  1.2,2.3,3.4]
 [2.1,2.2,2.3,
  3.1,3.2,3.4]]

My question is how to read it into R such like:

df <- data.frame(a=c(1.1,2.2,3.3,1.2,2.3,3.4),
                 b=c(2.1,2.2,2.3,3.1,3.2,3.4))
df
    a   b
1 1.1 2.1
2 2.2 2.2
3 3.3 2.3
4 1.2 3.1
5 2.3 3.2
6 3.4 3.4

Upvotes: 1

Views: 54

Answers (1)

akrun
akrun

Reputation: 887571

An option is reticulate

library(reticulate)
source_python('abc.txt')
data.frame(setNames(v1, c('a', 'b')))
#    a   b
#1 1.1 2.1
#2 2.2 2.2
#3 3.3 2.3
#4 1.2 3.1
#5 2.3 3.2
#6 3.4 3.4

data

#abc.txt
v1 = [[1.1,2.2,3.3,
  1.2,2.3,3.4],
 [2.1,2.2,2.3,
  3.1,3.2,3.4]]

Upvotes: 2

Related Questions