我有一个字符串字段:"[巴黎,马赛,荷兰]"
。我希望将此字符串转换为字符串列表。
目前我有以下函数:
def stringToList(string):
string = string[1:len(string)-1]
try:
if len(string) != 0:
tempList = string.split(", ")
newList = list(map(lambda x: str(x), tempList))
else:
newList = []
except:
newList = [-9999]
return(newList)
我想知道是否有一个更简单或更短的方法,可以得到相同的结果。 如果我的输入数据是
int
类型的,我可以使用ast.literal_eval()
。但在我的情况下,它不起作用。谢谢
"[Paris1, M4rseill3]"
将不起作用。因此,将\d
添加到混合物中可能不是一个坏主意。 - Ma0re
内部所做的。去除左右两侧、分割和有条件地选择元素可以在代码层面上简单地完成,但在内部却有些痛苦... 我想... - ghchoi