1

I made some calculations with my data and saved it into a csv file. In the file I have a cell with this string:

"[array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3])]"

I want to convert it to a valid numpy array. Tried some functions but got no luck yet.

3
  • 2
    Why do you think this is a reasonable way to serialize your array? Commented Sep 14, 2021 at 9:53
  • @juanpa.arrivillaga What do you mean? Commented Sep 14, 2021 at 9:55
  • Why do you think that putting this data into a text file like this, basically just dumping the string representation of a list, is a reasonable way to serialize your data? Why didn't you use something like JSON, or pickle? Commented Sep 14, 2021 at 9:55

1 Answer 1

2

If you have pandas, use pd.eval:

>>> import pandas as pd
>>> from numpy import array
>>> pd.eval("[array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3])]")
[array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3, 3, 3]), array([3])]
>>> 
4
  • I get the error: "array" is not a supported function Commented Sep 14, 2021 at 9:55
  • @Nikita Edited my answer with working code Commented Sep 14, 2021 at 9:55
  • If I ran it in Spyder I get the error "ValueError: unknown type object" But if I run it in the CMD everything's working right. Do you know what might be the issue? Commented Sep 14, 2021 at 10:11
  • Update: Just had to specify the engine as Python Commented Sep 14, 2021 at 10:26

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.