0

I have a string that looks identical to a list, let's say:

'[万福广场西,凰花苑]'

I would like to convert it into something like this:

['万福广场西','凰花苑']

I used eval() and ast.literal_eval() but got error as following:

y=eval('[万福广场西,凰花苑]')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-117-0aadbead218c> in <module>()
----> 1 y=eval('[万福广场西,凰花苑]')

<string> in <module>()

NameError: name '万福广场西' is not defined

when using ast.literal_eval(), I got this error ValueError: malformed node or string: <_ast.Name object at 0x000002A67FFA5CC0>

4
  • 1
    You got malformed node or string because it does NOT look identical to a list, it only look similar. Commented Aug 2, 2018 at 8:43
  • 2
    print( '[万福广场西,凰花苑]'.strip("[]").split(",") ) ? Commented Aug 2, 2018 at 8:45
  • @Stephen Rauch ok, how can I convert this to actual list? Commented Aug 2, 2018 at 8:46
  • @ Rakesh thanks for your answer Commented Aug 2, 2018 at 8:50

4 Answers 4

2

Try Something like

my_list = '[万福广场西,凰花苑]'[1:-1].split(",")

It will return you an list -- ['万福广场西', '凰花苑']

You can check it as -- type(my_list) #<class 'list'>

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

Comments

2

Use strip and split

>>> '[万福广场西,凰花苑]'.strip('[]').split(",")
['万福广场西', '凰花苑']

Comments

1

eval() will consider the given string as python code and return the result. So as per your string, the python code will something like this [万福广场西,凰花苑] which means two variable 万福广场西 and 凰花苑 are in a list.

If you want it to be evaluated as a list of strings you need to bound both strings with double quotes (") such as

'["万福广场西","凰花苑"]'.

When you subject this string to eval(), the output would be,

['万福广场西', '凰花苑']

If you want this to happen dynamically, you need to use split and join functions like,

''.join(list('[万福广场西,凰花苑]')[1:-1]).split(',')

which first makes the list of strings given by

list('[万福广场西,凰花苑]')[1:-1] # O/P ['万', '福', '广', '场', '西', ',', '凰', '花', '苑']

then joins all the strings as

''.join(['万', '福', '广', '场', '西', ',', '凰', '花', '苑']) # O/P 万福广场西,凰花苑

and splits the string by comma (,) to create your desired output.

'万福广场西,凰花苑'.split(',') # O/P ['万福广场西', '凰花苑']

Hope you have got what you were searching for. Feel free to comment in case of any clarifications required.

1 Comment

@Sidhom Welcome!
1

The content of your string isn't actually a valid list of literals because the literals are lacking the necessary quotes, so you can't parse it with eval() or ast.literal_eval().

Instead, you can use regular expression to parse the string into a list:

import re
print(re.findall(r'[^\[\],]+', '[万福广场西,凰花苑]'))

This outputs:

['万福广场西', '凰花苑']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.