0

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))
1
  • Please show what you have already tried. Commented Mar 16, 2017 at 12:41

2 Answers 2

4

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]]').

Sign up to request clarification or add additional context in comments.

2 Comments

it works!! may i ask about 'ast.literal_eval('[%s]' % l)' ?
@once Added some explanation in the post. Glad I could help!
-3

text = 'a,b,c'

text = text.split(',')

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.