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]]]
3
  • 2
    What does this csv file look like in plaintext? Commented Jul 2, 2022 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 and apply it to each element of the 2D array. Commented Jul 2, 2022 at 18:31
  • 1
    Try not to save arrays in that format. Parsing it isn't fun. Commented Jul 2, 2022 at 18:58

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

3 Comments

unnecessary use of exec when ast.literal_eval or json.loads would work.
Furthermore, I'm pretty sure genfromtext returns a numpy array, not a string.
@SuperStormer thanks, I've removed unnecessary use of exec()

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.