简体   繁体   English

存储 Python RegEx 多个组

[英]Storing Python RegEx multiple groups

I'm webscraping a site using python.我正在使用 python 抓取网站。 The returned results have the following format, ( https://regex101.com/r/irr14u/10 ), where everything works ok apart from the last occassion where i get 2 matches for the dates (1st match:Thur.-Sun., Tue., Wed. and second match: Mon.)返回的结果具有以下格式( https://regex101.com/r/irr14u/10 ),除了最后一次我获得 2 个日期匹配项(第一场比赛:Thur.-Sun. 、周二、周三和第二场比赛:周一)

I'm using the following code to get the values that i want.我正在使用以下代码来获取我想要的值。 I use BeautifoulSoup to get movieDate string, but here i hardcoded it.我使用 BeautifulSoup 来获取 movieDate 字符串,但在这里我对其进行了硬编码。

movieDate="Thur.-Sun., Tue., Wed.: 20.50/ 23.00, Mon. 23.00"

weekDays=re.match(',? *(?P<weekDays>[^\d:\n]+):? *(?P<startTime>[^,\n]+)', movieDate).groupdict()['weekDays']
startTime=re.match(',? *(?P<weekDays>[^\d:\n]+):? *(?P<startTime>[^,\n]+)', movieDate).groupdict()['startTime']

I want to create a dictionary as following (it has two keys because the are two startTime values);我想创建一个字典如下(它有两个键,因为它们是两个 startTime 值); The first key will be Thur.-Sun., Tue., Wed.第一个键将是周四至周日、周二、周三。 with value =20.50/ 23.00 and the second key will be Mon.值为 =20.50/ 23.00,第二个键是 Mon。 with value=23:00.值=23:00。 There might be occassions with one or more than two keys.有时可能会有一把或两把以上的钥匙。 So the dictionary will be as following;所以字典将如下;

dictionary={ Thur.-Sun., Tue., Wed.: 20.50/ 23.00, Mon.: 23.00}

Any suggestions to achieve that in a non boggy way?有什么建议可以以非沼泽的方式实现这一目标吗?

You can achieve the desired output using finditer function, appending result of the captured groups to a dict dynamically.您可以使用finditer函数实现所需的输出,将捕获的组的结果动态附加到 dict。

Python snippet: Python 片段:

import re
movieDate = """
Thur.-Sun., Tue., Wed.: 20.50/ 23.00, Mon. 23.00
"""

d = dict();
r = re.compile(',? *(?P<weekDays>[^\d:\n]+):? *(?P<startTime>[^,\n]+)')
for m in r.finditer(movieDate):
    d[m.group(1)] = m.group(2)

print(d)

Prints:印刷:

{'Thur.-Sun., Tue., Wed.': '20.50/ 23.00', 'Mon. ': '23.00'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM