0

Due to using a combination of not entirely compatible software packages, I need to be able to read a CSV file whose elements are stylized as Numpy arrays. An example of such a CSV would be the one below:

enter image description here

When using genfromtxt('Input.csv',delimiter=',',dtype=None,encoding=None), I get output of the form:

[['[4 3 2 1]' '[1 1 0 0]']
 ['[1 3 4 2]' '[0 1 1 0]']]

What I would like is to get this into the form of a three-dimensional Numpy array like below:

[[[4 3 2 1] [1 1 0 0]]
 [[1 3 4 2] [0 1 1 0]]]
econra2017
  • 67
  • 5
  • 2
    What does this csv file look like in plaintext? – SuperStormer Jul 02 '22 at 18:28
  • 1
    I'm not too familiar with numpy, but you should be able to use an answer from [convert string representation of array to numpy array in python](https://stackoverflow.com/questions/38886641/convert-string-representation-of-array-to-numpy-array-in-python) and apply it to each element of the 2D array. – SuperStormer Jul 02 '22 at 18:31
  • 1
    Try not to save arrays in that format. Parsing it isn't fun. – hpaulj Jul 02 '22 at 18:58

1 Answers1

1

You may create an array from these strings dynamically (at run-time):

import numpy as np
import ast

# data represents the string returned by str(genfromtxt(...))
data = '[[\'[4 3 2 1]\' \'[1 1 0 0]\'] [\'[1 3 4 2]\' \'[0 1 1 0]\']]'

# remove quotes
data = data.replace('\'', '')
# insert commas
data = data.replace(' ', ', ')

# create an array 
np_data = np.array(ast.literal_eval(data))

# here's your 3D numpy array
print(np_data.shape)
print(np_data)
Aaron Meese
  • 1,670
  • 3
  • 22
  • 32