2

I have a list and items are strings, but from some operation i got the list as string items are enclosed with extra "'string' ". How to get rid of that.

My code sample is

 import ast d = ["'WORKSHOP'", "'KIDS'", "'EXHIBITION'", "'FANTASY'", "'FESTIVAL'"] result = ast.literal_eval(d) ValueError: malformed node or string: ["'WORKSHOP'", "'KIDS'", "'EXHIBITION'", "'FANTASY'", "'FESTIVAL'"] 

I want the output as:

result = ['WORKSHOP', 'KIDS', 'EXHIBITION', 'FANTASY', 'FESTIVAL'] 

For integers:

res = [123, 345, 566, 78] 
2
  • Does it have to be with ast ? Commented Dec 7, 2015 at 6:01
  • @IronFist-please suggest if you have a new way!!!will be a great help. Commented Dec 7, 2015 at 6:02

2 Answers 2

6

Ah...you know, you're trying to use ast.literal_eval() on a list. So try this:

>>> import ast >>> d = ["'WORKSHOP'", "'KIDS'", "'EXHIBITION'", "'FANTASY'", "'FESTIVAL'"] >>> result = [ast.literal_eval(i) for i in d] >>> result ['WORKSHOP', 'KIDS', 'EXHIBITION', 'FANTASY', 'FESTIVAL'] >>> 

Or use map():

>>> import ast >>> d = ["'WORKSHOP'", "'KIDS'", "'EXHIBITION'", "'FANTASY'", "'FESTIVAL'"] >>> result = map(ast.literal_eval, d) >>> result ['WORKSHOP', 'KIDS', 'EXHIBITION', 'FANTASY', 'FESTIVAL'] >>> 

Remember on Python 3 it'll return a map object, to covert it to list just use list(map(ast.literal_eval, d)).


Another way to solve this is simply remove the first char and the last char in the string since they're already string objects:

>>> d = ["'WORKSHOP'", "'KIDS'", "'EXHIBITION'", "'FANTASY'", "'FESTIVAL'"] >>> [i[1:-1] for i in d] ['WORKSHOP', 'KIDS', 'EXHIBITION', 'FANTASY', 'FESTIVAL'] >>> list(map(lambda x: x[1:-1], d)) ['WORKSHOP', 'KIDS', 'EXHIBITION', 'FANTASY', 'FESTIVAL'] >>> 
Sign up to request clarification or add additional context in comments.

6 Comments

You can simply use map(ast.literal_eval,d)
@Kevin-sorry for this kind of childish question.Will be careful further.Thanks.
@Satya: No problem :)
Can it be dynamic.Means the list that are initialise first and then getting used, those should come dynamically(from program (filtering from that total list)) and assigned automatically after all computaion using dictionary mapping or something.
|
1

Why not simply:

>>> d = ["'WORKSHOP'", "'KIDS'", "'EXHIBITION'", "'FANTASY'", "'FESTIVAL'"] >>> >>> >>> l = map(lambda s:s.replace('\'', ''), d) >>> l ['WORKSHOP', 'KIDS', 'EXHIBITION', 'FANTASY', 'FESTIVAL'] 

Also, you can do it this way with string module:

>>> d = ["'WORKSHOP'", "'KIDS'", "'EXHIBITION'", "'FANTASY'", "'FESTIVAL'"] >>> allchars =string.maketrans('','') #to make a chars list of 256, for translate method >>> l = map(lambda s:s.translate(allchars, '\''),d) >>> l ['WORKSHOP', 'KIDS', 'EXHIBITION', 'FANTASY', 'FESTIVAL'] 

4 Comments

What will happen if the string is "'WO'RK'S'HOP'" and OP wants save these ' except the around pair?
And the string version doesn't work if you're using Python 3.x.
@KevinGuan, it will indeed remove the ' char from "'WO'RK'S'HOP'" giving out: 'WORKSHOP'
@KevinGuan...you are right...I didn't think that OP wanted only to remove ' on the sides...Thanks for the head's up

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.