vijay Dave
vijay Dave

Reputation: 51

Extracting list from string

I am using python and facing problem to extract specific list object from string which has that list.

Here is my list object within a string.

input = "[[1,2,3],[c,4,r]]"

I need output like this.

output = [[1,2,3],[c,4,r]]

is there any way to do this?

Upvotes: 5

Views: 11151

Answers (5)

pieterbons
pieterbons

Reputation: 1724

I use the pandas function 'read_json' to interpret data structures stored as a string. In this example you can use typ = 'series' to interpret the structure as a pandas series and then change it back into a list:

input = "[[1,2,3],[4,5,6]]"
pd.read_json(input, typ = 'series').to_list()

However, for some reason it only works with numeric content, not with your example of a list containing numbers and characters.

Upvotes: 0

M Newville
M Newville

Reputation: 7862

if ast.literal_eval is not sufficient and c and r are meant to be non-trivial data objects that should be interpreted, you might consider using asteval (https://github.com/newville/asteval). It can handle evaluating python strings beyond literal strings, including its own symbol table, and avoiding many of the known exploits with using eval().

asteval works like a sand-boxed mini-interpreter with a simple, flat namespace. You would have to add values for c and r to the asteval interpreter, but an example might be:

from asteval import Interpreter
aeval = Interpreter()
aeval('c = 299792458.0')   # speed of light?
aeval('r = c/(2*pi)')      # radius of a circle of circumference c?
                           # note that pi and many other numpy
                           # symbols and functions are built in
input_string = "[[1,2,3],[c,4,r]]"

out = aeval(input_string)
print(out)

which will give

[[1, 2, 3], [299792458.0, 4, 47713451.59236942]]

Alternatively, you could have set c directly from Python with

aeval.symtable['c'] = 299792458.0

There are many known-to-be-unsafe things that asteval refuses to do, but there are also many things it can do that ast.literal_eval cannot. Of course, if you're accepting code from user input, you should be very careful.

Upvotes: 2

Aaditya Ura
Aaditya Ura

Reputation: 12689

Never use input as variable name , it's a keyword in python :

You can use numpy :

import numpy as np

user_input = "[[1,2,3],[c,4,r]]"

print(np.array(user_input))

output:

[[1,2,3],[c,4,r]]

Upvotes: -2

Ramineni Ravi Teja
Ramineni Ravi Teja

Reputation: 3926

Use ast module literal_eval method for a safer eval

import ast
input = "[[1,2,3],['c',4,'r']]"
output= ast.literal_eval(input)
print(output)
#[[1, 2, 3], ['c', 4, 'r']]

This code is tested in Python 3.6. It works even in 2.7 also.

Upvotes: 0

Use ast.literal_eval:

Python 2.7.10 (default, Jul 14 2015, 19:46:27)
[GCC 4.8.2] on linux
   import ast
   input = "[[1,2,3],['c',4,'r']]"
   output = ast.literal_eval(input)
   output
=> [[1, 2, 3], ['c', 4, 'r']]

If you actually meant for c and r to just be the current values of the variables c and r rather than the literals 'c' and 'r', you'd need to use eval, which is rather unsafe, but otherwise works the same way.

Upvotes: 6

Related Questions