简体   繁体   English

将列表的字符串表示形式转换为Python中的列表

[英]Convert string representation of list to list in Python

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: 我使用了eval()ast.literal_eval()但收到如下错误:

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> 使用ast.literal_eval() ,出现以下错误ValueError: malformed node or string: <_ast.Name object at 0x000002A67FFA5CC0>错误的ValueError: malformed node or string: <_ast.Name object at 0x000002A67FFA5CC0>

Try Something like 尝试类似的东西

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

It will return you an list -- ['万福广场西', '凰花苑'] 它将返回一个列表- ['万福广场西', '凰花苑']

You can check it as -- type(my_list) #<class 'list'> 您可以将其检查为type(my_list) #<class 'list'>

Use strip and split 使用stripsplit

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

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() . 字符串的内容实际上不是有效的文字列表,因为文字缺少必要的引号,因此您不能使用eval()ast.literal_eval()对其进行解析。

Instead, you can use regular expression to parse the string into a list: 相反,您可以使用正则表达式将字符串解析为列表:

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

This outputs: 输出:

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

eval() will consider the given string as python code and return the result. eval()会将给定的字符串视为python代码并返回结果。 So as per your string, the python code will something like this [万福广场西,凰花苑] which means two variable 万福广场西 and 凰花苑 are in a list. 因此,按照您的字符串,python代码将类似于此[万福广场西,凰花苑] ,这意味着列表中有两个变量万福广场西凰花苑

If you want it to be evaluated as a list of strings you need to bound both strings with double quotes (") such as 如果要将其作为字符串列表进行评估,则需要将两个字符串都用double quotes (")进行绑定,例如

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

When you subject this string to eval(), the output would be, 当将此字符串作为eval()的对象时,输出为

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

If you want this to happen dynamically, you need to use split and join functions like, 如果您希望动态地进行此操作,则需要使用split和join函数,例如,

''.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. 并用comma (,)分割字符串以创建所需的输出。

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

Hope you have got what you were searching for. 希望您能找到想要的东西。 Feel free to comment in case of any clarifications required. 如有必要,请随时发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM