我想知道最简单的方法是将如下列表的字符串表示转换为列表:
x = '[ "A","B","C" , " D"]'
即使在用户在逗号之间加空格,在引号内加空格的情况下,我也需要处理它并将其转换为:
x = ["A", "B", "C", "D"]
我知道我可以用strip()和split()分隔空格,并检查非字母字符。但代码变得非常笨拙。有没有我不知道的快速功能?
我想知道最简单的方法是将如下列表的字符串表示转换为列表:
x = '[ "A","B","C" , " D"]'
即使在用户在逗号之间加空格,在引号内加空格的情况下,我也需要处理它并将其转换为:
x = ["A", "B", "C", "D"]
我知道我可以用strip()和split()分隔空格,并检查非字母字符。但代码变得非常笨拙。有没有我不知道的快速功能?
当前回答
在处理存储为Pandas DataFrame的报废数据时,可能会遇到这样的问题。
如果值列表以文本形式显示,则此解决方案非常有用。
def textToList(hashtags):
return hashtags.strip('[]').replace('\'', '').replace(' ', '').split(',')
hashtags = "[ 'A','B','C' , ' D']"
hashtags = textToList(hashtags)
Output: ['A', 'B', 'C', 'D']
不需要外部库。
其他回答
当您将存储为字符串的列表加载到CSV时,通常会发生这种情况
如果您的列表以CSV格式存储,如OP所问:
x = '[ "A","B","C" , " D"]'
以下是如何将其加载回列表:
import csv
with open('YourCSVFile.csv') as csv_file:
reader = csv.reader(csv_file, delimiter=',')
rows = list(reader)
listItems = rows[0]
listItems现在是列表
只需从列表的字符串表示形式中切下第一个和最后一个字符,即可保存.strip()函数(请参见下面的第三行):
>>> mylist=[1,2,3,4,5,'baloney','alfalfa']
>>> strlist=str(mylist)
['1', ' 2', ' 3', ' 4', ' 5', " 'baloney'", " 'alfalfa'"]
>>> mylistfromstring=(strlist[1:-1].split(', '))
>>> mylistfromstring[3]
'4'
>>> for entry in mylistfromstring:
... print(entry)
... type(entry)
...
1
<class 'str'>
2
<class 'str'>
3
<class 'str'>
4
<class 'str'>
5
<class 'str'>
'baloney'
<class 'str'>
'alfalfa'
<class 'str'>
不需要导入任何内容或进行评估。对于大多数基本用例,包括原始问题中给出的用例,您可以在一行中完成此操作。
一个衬垫
l_x = [i.strip() for i in x[1:-1].replace('"',"").split(',')]
解释
x = '[ "A","B","C" , " D"]'
# String indexing to eliminate the brackets.
# Replace, as split will otherwise retain the quotes in the returned list
# Split to convert to a list
l_x = x[1:-1].replace('"',"").split(',')
输出:
for i in range(0, len(l_x)):
print(l_x[i])
# vvvv output vvvvv
'''
A
B
C
D
'''
print(type(l_x)) # out: class 'list'
print(len(l_x)) # out: 4
您可以根据需要使用列表理解来解析和清理此列表。
l_x = [i.strip() for i in l_x] # list comprehension to clean up
for i in range(0, len(l_x)):
print(l_x[i])
# vvvvv output vvvvv
'''
A
B
C
D
'''
嵌套列表
如果您有嵌套列表,它确实会变得有点烦人。如果不使用正则表达式(这将简化替换),并且假设您希望返回一个扁平列表(python的zen表示扁平优于嵌套):
x = '[ "A","B","C" , " D", ["E","F","G"]]'
l_x = x[1:-1].split(',')
l_x = [i
.replace(']', '')
.replace('[', '')
.replace('"', '')
.strip() for i in l_x
]
# returns ['A', 'B', 'C', 'D', 'E', 'F', 'G']
如果您需要保留嵌套列表,它会变得有点难看,但仍然可以通过正则表达式和列表理解来完成:
import re
x = '[ "A","B","C" , " D", "["E","F","G"]","Z", "Y", "["H","I","J"]", "K", "L"]'
# Clean it up so the regular expression is simpler
x = x.replace('"', '').replace(' ', '')
# Look ahead for the bracketed text that signifies nested list
l_x = re.split(r',(?=\[[A-Za-z0-9\',]+\])|(?<=\]),', x[1:-1])
print(l_x)
# Flatten and split the non nested list items
l_x0 = [item for items in l_x for item in items.split(',') if not '[' in items]
# Convert the nested lists to lists
l_x1 = [
i[1:-1].split(',') for i in l_x if '[' in i
]
# Add the two lists
l_x = l_x0 + l_x1
最后一个解决方案可以处理任何以字符串形式存储的列表,无论是否嵌套。
这是你能做到的,
**
x = '[ "A","B","C" , " D"]'
print(list(eval(x)))
**最好的答案是公认的答案
虽然这不是一个安全的方法,但最好的答案是公认的。当答案发布时,我并没有意识到评估的危险。
使用纯Python-不导入任何库:
[x for x in x.split('[')[1].split(']')[0].split('"')[1:-1] if x not in[',',' , ',', ']]