1

I have a files in which data is something like this

[1,2,3,4,5]
[4,2,3,4,5,6]
[3,12,3,34,5]
[3,4,5,6,7,8]
[2,3,4,5,6,6]
[2,3,4,5,5,4,3]
[1,2,3,23,2,1]

i just want to convert this into numpy array, something like this

[[1,2,3,4,5],[4,2,3,4,5,6]]

I tried following code:

import ast
import numpy as np
a = np.array([])
with open("test.txt","r") as f:
        for line in f:
                line = ast.literal_eval(line)
                np.append(line,a)

it is only giving []

4
  • 1
    I tried following code: It might prove useful if you also were to divulge what happened next. Commented Feb 16, 2018 at 6:03
  • @PaulPanzer sure editing Commented Feb 16, 2018 at 6:09
  • 1
    Btw. I think you want to swap arguments of append and more importantly reassign to a. np.append does not work in-place. Commented Feb 16, 2018 at 6:15
  • 1
    BTW, this isn't going to be a proper array, since the dimensions aren't regular. Commented Feb 16, 2018 at 6:22

2 Answers 2

1

Using array method in numpy.

import ast
from numpy import array

p = "PATH_TO_INPUT_FILE"
data = []
with open(p,"r") as f:
    for i in f.readlines():
        data.append(ast.literal_eval(i.strip()))
data = array(data)
print(data)
print(type(data))

Output:

[[1, 2, 3, 4, 5] [4, 2, 3, 4, 5, 6] [3, 12, 3, 34, 5] [3, 4, 5, 6, 7, 8]
 [2, 3, 4, 5, 6, 6] [2, 3, 4, 5, 5, 4, 3] [1, 2, 3, 23, 2, 1]]
<type 'numpy.ndarray'>
Sign up to request clarification or add additional context in comments.

Comments

0
with open('file.txt') as f:
    data = []
    for line in f:
        words = line.replace('[', ''). replace(']', '').replace('\n', '').split(',')
        data.append([int(i) for i in words])

if you want numpy array:

with open('file.txt') as f:
    data = []
    for line in f:
        words = line.replace('[', ''). replace(']', '').replace('\n', '').split(',')
        data.append(np.array([int(i) for i in words]))

3 Comments

it is giving: [array([1, 2, 3, 4, 5]), array([4, 2, 3, 4, 5, 6]), array([ 3, 12, 3, 34, 5]), array([3, 4, 5, 6, 7, 8]), array([2, 3, 4, 5, 6, 6]), array([2, 3, 4, 5, 5, 4, 3]), array([ 1, 2, 3, 23, 2, 1])]
The first one will give you a list of lists (which is what you wanted in your question). The second one will produce list of np arrays. Use np.array(data) to the first output to get 2d np array
@ggupta What do you expect from numpy? Your lists don't have the same dimension, how are they supposed to be converted into an n x m array?

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.