3

Hi doing some stuff over a network and wondering if there is any way of converting python array as a string back into a python array.. for example

x = "[1,2,3,4]"

converting x to

x_array = [1,2,3,4]

Bonus if it can also work for numpy multidimensional arrays!

0

5 Answers 5

8

For the normal arrays, use ast.literal_eval:

>>> from ast import literal_eval
>>> x = "[1,2,3,4]"
>>> literal_eval(x)
[1, 2, 3, 4]
>>> type(literal_eval(x))
<type 'list'>
>>>

numpy.array's though are a little tricky because of how Python renders them as strings:

>>> import numpy as np
>>> x = [[1,2,3], [4,5,6]]
>>> x = np.array(x)
>>> x
array([[1, 2, 3],
       [4, 5, 6]])
>>> x = str(x)
>>> x
'[[1 2 3]\n [4 5 6]]'
>>>

One hack you could use though for simple ones is replacing the whitespace with commas using re.sub:

>>> import re
>>> x = re.sub("\s+", ",", x)
>>> x
'[[1,2,3],[4,5,6]]'
>>>

Then, you can use ast.literal_eval and turn it back into a numpy.array:

>>> x = literal_eval(x)
>>> np.array(x)
array([[1, 2, 3],
       [4, 5, 6]])
>>>
Sign up to request clarification or add additional context in comments.

Comments

3
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
>>> import json
>>> json.loads("[1,2,3,4]")
[1, 2, 3, 4]
>>> 

5 Comments

why do you both answer and mark as duplicate?
Also, this answer is already mentioned in the duplicated thread.
@njzk2 Oh, I didn't see the part of the other answer that mentioned JSON.
@njzk2, you should read the answer in the linked question :) "The question should be closed (and later deleted), but go ahead and give the asker the answer they were looking for first so we don't leave them with an absolutely horrible experience for it."
1

I would suggest that you don't actually want to do this. This kind of thing isn't going to be scalable easily to sending, for instance, instances of classes you've defined across a network. Instead, I would suggest that you use something like pickle or json to convert the data to bytes, send it across the network, and then convert the data back. However, in other situations I would always use ast.literal_eval. If you specify how you are sending the data across the network, I'll give you example usage of pickle for your situation.

Comments

1

Try this:

x_array = [(int(y) if y.strip() else None) for y in x[1:-1].split(',')]

1 Comment

+1 However, although I see what you are trying to do, I think you should include a list(filter(lamda x: x is not None, around this to strip the None values.
1

If you're sure the strings are always going to have that structure, you could remove the brackets and split the string:

x_array = [int(y) for y in x[1:-1].split(',') if y.strip()]

3 Comments

akaRem ?? That's invalid syntax for a list. No one cares. Strings are suppossed to hold valid list representations.
Breaks on x = "[1, 2, 3, 4, 5, ]".
Fixed it using strip to check for empty strings.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.