I'm using the following code to find all the indexes from index 0 to ~ so I could create a row off of each match
import re
s = 'product 1 & product 2|category 1|8~product 4|category 3 |10~product 1 & product 19|category 8|6~product 50|category 4|6'
substring = "~"
matches = re.finditer(substring, s)
matches_positions = [match.start() for match in matches]
print(matches_positions)
output
[34, 59, 95]
I'm manually using every index in order to show the following output, I would like to create a function that could split and return each occurrence
print(s[0:34])
print(s[34 + 1: 59])
print(s[59 +1 : 95])
print(s[95 +1 : len(s)])
output
product 1 & product 2|category 1|8
product 4|category 3 |10
product 1 & product 19|category 8|6
product 50|category 4|6
Thank you beforehand
You just need to use the str.split
built-in function:
outputs = s.split('~')
Here is outputs
value:
['product 1 & product 2|category 1|8',
'product 4|category 3 |10',
'product 1 & product 19|category 8|6',
'product 50|category 4|6']
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.