1import re
2
3def parse_wishlist(raw_data):
4 regex = r'\(\s*([\d\.]+)\s*\)\s*([^\[\]]+?)\s*\[\s*(\d+).*\]((?:\n|.)+?)(?=(?:\n\(|$))'
5 raw_parsed_data = re.findall(regex, raw_data)
6 parsed_data = []
7 for entry in raw_parsed_data:
8 niceness, name, age, wishlist = entry
9 wishlist = tuple(re.findall(r'^[\t\f ]*[\-\*][\t\f ]*(.+)[\t\f ]*', wishlist, re.M))
10 parsed_data.append((float(niceness), name, float(age), wishlist))
11 return parsed_data
12
13# Alternatively, we can always do this:
14# [(float(match[0], match[1], float(match[2], re.findall(r'^[\t\f ]*[\-\*][\t\f ]*(.+)[\t\f ]*', match[3], re.M)) for match in re.findall(r'\(\s*([\d\.]+)\s*\)\s*([^\[\]]+?)\s*\[\s*(\d+).*\]((?:\n|.)+?)(?=(?:\n\(|$))', s)]
...
----------------------------------------------------------------------
Ran 3 tests in 0.002s
OK
f | 1 | import re | f | 1 | import re |
2 | 2 | ||||
3 | def parse_wishlist(raw_data): | 3 | def parse_wishlist(raw_data): | ||
4 | regex = r'\(\s*([\d\.]+)\s*\)\s*([^\[\]]+?)\s*\[\s*(\d+).*\]((?:\n|.)+?)(?=(?:\n\(|$))' | 4 | regex = r'\(\s*([\d\.]+)\s*\)\s*([^\[\]]+?)\s*\[\s*(\d+).*\]((?:\n|.)+?)(?=(?:\n\(|$))' | ||
5 | raw_parsed_data = re.findall(regex, raw_data) | 5 | raw_parsed_data = re.findall(regex, raw_data) | ||
6 | parsed_data = [] | 6 | parsed_data = [] | ||
7 | for entry in raw_parsed_data: | 7 | for entry in raw_parsed_data: | ||
8 | niceness, name, age, wishlist = entry | 8 | niceness, name, age, wishlist = entry | ||
9 | wishlist = tuple(re.findall(r'^[\t\f ]*[\-\*][\t\f ]*(.+)[\t\f ]*', wishlist, re.M)) | 9 | wishlist = tuple(re.findall(r'^[\t\f ]*[\-\*][\t\f ]*(.+)[\t\f ]*', wishlist, re.M)) | ||
n | 10 | parsed_data.append((niceness, name, age, wishlist)) | n | 10 | parsed_data.append((float(niceness), name, float(age), wishlist)) |
11 | return parsed_data | 11 | return parsed_data | ||
12 | 12 | ||||
13 | # Alternatively, we can always do this: | 13 | # Alternatively, we can always do this: | ||
t | 14 | # [match[:3] + (re.findall(r'^[\t\f ]*[\-\*][\t\f ]*(.+)[\t\f ]*', match[3], re.M), ) for match in re.findall(r'\(\s*([\d\.]+)\s*\)\s*([^\[\]]+?)\s*\[\s*(\d+).*\]((?:\n|.)+?)(?=(?:\n\(|$))', s)] | t | 14 | # [(float(match[0], match[1], float(match[2], re.findall(r'^[\t\f ]*[\-\*][\t\f ]*(.+)[\t\f ]*', match[3], re.M)) for match in re.findall(r'\(\s*([\d\.]+)\s*\)\s*([^\[\]]+?)\s*\[\s*(\d+).*\]((?:\n|.)+?)(?=(?:\n\(|$))', s)] |
Legends | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
|