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.
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 ...
[("#<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
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>')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.