This question already has an answer here:

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!

marked as duplicate by Bakuriu, warvariuc, iCodez, Ashwini Chaudhary, njzk2 Nov 28 '13 at 20:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 5 down vote accepted

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]])
>>>

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.

  • +1 for mentioning pickle. – tawmas Nov 28 '13 at 19:59

Try this:

x_array = [(int(y) if y.strip() else None) for y in x[1:-1].split(',')]
  • +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. – rlms Nov 28 '13 at 20:06
Python 2.7.5+ (default, Sep 19 2013, 13:48:49) 
>>> import json
>>> json.loads("[1,2,3,4]")
[1, 2, 3, 4]
>>> 
  • 1
    why do you both answer and mark as duplicate? – njzk2 Nov 28 '13 at 20:07
  • please read this meta.stackexchange.com/questions/4283/… – njzk2 Nov 28 '13 at 20:09
  • Also, this answer is already mentioned in the duplicated thread. – njzk2 Nov 28 '13 at 20:10
  • @njzk2 Oh, I didn't see the part of the other answer that mentioned JSON. – rlms Nov 28 '13 at 20:19
  • 1
    @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." – warvariuc Nov 28 '13 at 20:19

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()]
  • "[1,,2]" != [1, 2] – akaRem Nov 28 '13 at 19:54
  • akaRem ?? That's invalid syntax for a list. No one cares. Strings are suppossed to hold valid list representations. – José Tomás Tocino Nov 28 '13 at 19:57
  • Ooops.......... – akaRem Nov 28 '13 at 20:00
  • Breaks on x = "[1, 2, 3, 4, 5, ]". – rlms Nov 28 '13 at 20:07
  • Fixed it using strip to check for empty strings. – José Tomás Tocino Nov 28 '13 at 20:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.