once
once

Reputation: 1399

convert string into multidimensional array

i want to feed my data into a (n,4,5) python numpy array. are there any simple solutions?

i've format my data so that each line of the file looks like a python array, but its hard to read it as a python array, for example:

[0,0,0,1,1],[0,0,0,0,0],[0,1,1,0,0],[1,0,0,0,0] //line1
[1,0,0,1,0],[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0] //line2
... 

desire output:

myarray=[[[0,0,0,1,1],[0,0,0,0,0],[0,1,1,0,0],[1,0,0,0,0]],[[1,0,0,1,0],[0,1,0,0,0],[0,0,1,0,0],[0,0,0,1,0]]...]

seems strip, eval and json all not working well.. please help

i've also tried:

with open('filename') as f:
    data = f.readlines()
    data = [x.strip() for x in data]
array=[]
for i in data:
    a=split(r'(?<=\]),(?=\[)',i )
    array.append(a)
data=np.array((array))

Upvotes: 0

Views: 4119

Answers (2)

AKX
AKX

Reputation: 168913

Wrap each line in one more pair of brackets, then pass to a suitable eval function:

import ast

arr = []
with open('input.txt', 'r') as infp:
    for l in infp:
        arr.append(ast.literal_eval('[%s]' % l))  # replace with eval() if you trust your input data

print(arr)      

Output:

[[[0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [1, 0, 0, 0, 0]], [[1, 0, 0, 1, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]]]

And a little explanation, as requested:

  • Since each line in the input file is of the form [1,2],[3,4] and a Python list-of-lists would be [[1, 2], [3, 4]], '[%s]' is used to wrap the line in that one more pair of brackets to make it valid Python.
  • ast.literal_eval() is a safe form of eval() that only accepts literals (no function calls or other such things).

  • So all in all, for a line [1, 2], [3, 4], the effective code is eval('[[1, 2], [3, 4]]').

Upvotes: 4

Ashwin Golani
Ashwin Golani

Reputation: 411

text = 'a,b,c'

text = text.split(',')

text [ 'a', 'b', 'c' ]

Upvotes: -3

Related Questions