Rushi Jae
Rushi Jae

Reputation: 1

Is there a better way to read matrices with numpy?

I have text file which contains matrices like this

[ 1 2 3 4 5 6 7 8 9 10
  11 12 13] [ 14 15 16
  17 18 19 20 21 22 23]

I need a faster way to read the matrices into numpy. I used reg to split the matrix with spaces, and ']' then separately read into a numpy array, which is a tedious process. I need to store it in numpy like this:

[[ 1 2 3 4 5 6 7 8 9 10 11 12 13], [ 14 15 16 17 18 19 20 21 22 23]]

Upvotes: 0

Views: 55

Answers (1)

jpp
jpp

Reputation: 164673

You can't get this in numpy as your array has rows of different length.

This is a brute-force way to transform your input into a list of lists:

import pandas as pd
from io import StringIO
from ast import literal_eval

mystr = StringIO("""[ 1 2 3 4 5 6 7 8 9 10
  11 12 13] [ 14 15 16
  17 18 19 20 21 22 23]""")

# replace mystr with 'file.csv'
df = pd.read_csv(mystr, header=None)

res = '[' + ''.join([x.replace('  ', ',').replace('[ ', '[')\
                     .replace(' ', ',').replace('][', '],[') for x in df[0]]) + ']'

lst = literal_eval(res)

# [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
#  [14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]

Upvotes: 1

Related Questions