[英]How to parse a string without comma to python list?
I want to parse a string like this to a python list:我想将这样的字符串解析为 python 列表:
"[[ 3.14 2.12] [1.11 2.22 ] [10.0 12.0]]" # Not separated by comma
I tried to use eval()
function, but it failed with invalid syntax.我尝试使用
eval()
function,但由于语法无效而失败。 Is there another way to easily parse such a string into python list?有没有另一种方法可以轻松地将这样的字符串解析为 python 列表?
What I hope to achieve is to obtain a real python list like this:我希望实现的是获得一个真正的 python 列表,如下所示:
mylist = [[ 3.14, 2.12], [1.11, 2.22 ], [10.0, 12.0]]
and mylist[0]
will return [3.14, 2.12] and mylist[0][1]
will return 2.12. mylist[0]
将返回 [3.14, 2.12] 并且mylist[0][1]
将返回 2.12。
This simply replaces the empty spaces with comma and then using the ast
module you can convert a string represtntaion of lists of list into list.这只是用逗号替换空格,然后使用
ast
模块,您可以将列表列表的字符串表示形式转换为列表。
import ast
h = "[[3.14 2.12] [1.11 2.22] [10.0 12.0]]"
a = str(h).replace( ' ' , ',' )
x = ast.literal_eval(a)
print(x)
Output Output
[[3.14, 2.12], [1.11, 2.22], [10.0, 12.0]] # of type lists of list
Also, note that this is rather a naive approach since this requires the string representation of lists of the list to be in the correct format.另外,请注意,这是一种相当幼稚的方法,因为这需要列表的列表的字符串表示形式正确。
Eg this is valid [[3.14 2.12] [1.11 2.22] [10.0 12.0]]
,whereas [[ 3.14 2.12] [1.11 2.22 ] [10.0 12.0]]
this is not due to the presence of empty spaces here [[ 3.14]
for example.例如,这是有效的
[[3.14 2.12] [1.11 2.22] [10.0 12.0]]
,而[[ 3.14 2.12] [1.11 2.22 ] [10.0 12.0]]
这不是因为此处存在空格[[ 3.14]
例子。
re.sub
can be used to add a comma after each nested list and number. re.sub
可用于在每个嵌套列表和数字之后添加逗号。
import re
s = "[[ 3.14 2.12] [1.11 2.22 ] [10.0 12.0]]"
def add_comma(match):
return match.group(0) + ','
s = re.sub(r'\[[0-9\.\s]+\]', add_comma, s)
s = re.sub(r'([0-9\.]+)', add_comma, s)
mylist = eval(s)
x= "[[3.14 2.12] [1.11 2.22] [10.0 12.0]]"
z=[y.strip().split(']')[0].split(' ') for y in x.split('[') if y!='']
Explanation: Splitting x on说明:拆分 x on
'[' produces-->['', '', '3.14 2.12] ', '1.11 2.22] ', '10.0 12.0]]']
Using condition if y!='' on this split produces在此拆分上使用条件 if y!='' 会产生
['3.14 2.12] ', '1.11 2.22] ', '10.0 12.0]]']
Using y.strip() removes the extra whitespaces in the end of each element.使用 y.strip() 删除每个元素末尾的额外空格。 Stripping on ']' produces
剥离 ']' 产生
[['3.14 2.12', ''], ['1.11 2.22', ''], ['10.0 12.0', '', '']]
Now, as we can see only the 1st element has our values, take index =0 & split on '' produces现在,我们可以看到只有第一个元素有我们的值,取 index =0 & split on '' 产生
[['3.14', '2.12'], ['1.11', '2.22'], ['10.0', '12.0']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.