1

Let's say I have a numpy array a = numpy.array([1,2,3,4]). Now str(a) will give me "[1 2 3 4]". How do I convert the string "[1 2 3 4]" back to a numpy.array([1,2,3,4])?

3
  • I would ask why you want to do that instead of just keeping the array, but you can "split" on the spaces. arr = numpy.array(s.split(' ')) Commented Nov 22, 2019 at 11:25
  • @ChristianSloper I am writing these numpy arrays to csv files (gets written as shown in example) and i want to be able to read them back as numpy arrays Commented Nov 22, 2019 at 11:27
  • Use savetext() from numpy to store them properly Commented Nov 22, 2019 at 11:29

1 Answer 1

2

Try numpy.array([int(v) for v in your_str[1:-1].split()])

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.