[英]how to use the regex findall list
so i'm a js trainee and at my internship someone asked me to do something on the python code, but i never did something on Python so i'm a bit lost.. I would like to separate a string in differents block. 所以我是一名JS培训生,在实习期间,有人要求我对python代码做一些事情,但是我从未在Python上做过什么,所以我有点迷路了。我想将字符串分隔为differents块。
here is what i have : 这是我所拥有的:
buffer = """
#<start>
idothings
#</start>
#<params>
otherthings
#</params>
#<end>
andifinish
#</end>
what i would like is a regex that separate this string in differents parts : 我想要的是将这个字符串分为不同部分的正则表达式:
separatedString = [["#<start>,"idothings","#</start>"],["#<params>,"otherthings","#</params>"],["#<end>,"andifinish","#</end>"]]
what i tried to do is that : 我试图做的是:
def getStructure(string):
separatedString = re.findall('(#<.+?>)(.|\n)+?(#<\/.+?>)', string)
return
but this gave me a list... and i don't understand how do i navigate through a list in python ... 但这给了我一个列表...我不明白如何在python中浏览列表...
[("#<start>", '\n', '#</start>'), ('#<azeaze>', '\n', '#</azeaze>'), ('#<sgdfs>', 'x', '#</sgdfs>')]
i tried : 我试过了 :
print '\n'.join(["%s a %s et %s" %(p1,p2,p3) for p1, strings in separatedString ])
but it game me an error "too many values to unpack" 但它给我带来了一个错误:“无法解开太多的价值”
Can someone show me how i can do this? 有人可以告诉我我该怎么做吗?
Your print statement is a little faulty . 您的印刷声明有误。 Try this instead
试试这个
print '\n'.join(["%s a %s et %s" %(p1,p2,p3) for p1, p2, p3 in separatedString ])
You are getting an error because, you are trying to get two values from a tuple having three elements 您收到错误是因为,您试图从具有三个元素的元组中获取两个值
for p1, strings in separatedString
Here separatedString
has 3 elements in each of its members 在这里,
separatedString
字符串在每个成员中都有3个元素
buffer = """#<start>
idothings
#</start>
#<params>
otherthings
#</params>
#<end>
andifinish
#</end>"""
spl = buffer.splitlines()
print([spl[i:i+3] for i in range(0,len(spl),3)])
[['#<start>', ' idothings', '#</start>'], ['#<params>', ' otherthings', '#</params>'], ['#<end>', ' andifinish', '#</end>']]
spl = buffer.splitlines()
sliced = [spl[i:i+3] for i in range(0,len(spl),3)]
for a,b,c in sliced:
print(a.strip(),b.strip(),c.striip())
('#<start>', 'idothings', '#</start>')
('#<params>', 'otherthings', '#</params>')
('#<end>', 'andifinish', '#</end>')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.