python 如何處理提取嵌套中括弧 [] 的內容?
01-30
import re
b = "[[1,2,344],[323,3,34]]"regex1 = re.compile(r[[(.*)]])
print (regex1.findall(b))
#提取括弧內的數字,保存順序不變。
"""方法一"""
str_ = "[[1,2,344],[323,3,34]]"
list_ = eval(str_)
[i for k in list_ for i in k]
""""方法二"""
import re
string = "[[1,2,344],[323,3,34]]"
regex = re.compile(d+)
[int(i) for i in re.findall(regex, string)]
我也來一個吧:
&>&>&> b = "[[1,2,344],[323,3,34]]"
&>&>&> b.replace([,).replace(],).split(,)
[1, 2, 344, 323, 3, 34]
推薦閱讀:
TAG:Python |