TGTGInsighttelegram intelligenceLIVE / telegram public index
← Python Заметки

TGINSIGHT SIMILAR POSTS

Најди сличен содржај

Изворен канал @pythonotes · Post #65 · 8 апр.

Небольшой трик с регулярными выражениями который редко вижу в чужом коде. Допустим, вам нужно распарсить простой текст и вытащить оттуда пары имя+телефон. Вернуть всё это надо в виде списка словарей. Возьмем очень простой пример текста. >>> text = ''' >>> Alex:8999123456 >>> Mike:+799987654 >>> Oleg:+344456789 >>> ''' Соответственно, для выделения нужных элементов будем использовать группы. Получится такой паттерн: (\w+):([\d+]+) Как мы будем формировать словарь из найденных групп? >>> import re >>> results = [] >>> for match in re.finditer(r"(\w+):([\d+]+)", text): >>> results.append({ >>> "name": match.group(1), >>> "phone": match.group(2) >>> }) >>> print(results) [{'name': 'Alex', 'phone': '8999123456'}, ...] Можно немного сократить запись используя zip >>> results = [] >>> for match in re.finditer(r"(\w+):([\d+]+)", text): >>> results.append(dict(zip(['name', 'phone'], match.groups()))) Но есть способ лучше! Это именованные группы в regex. Можно в паттерне указать имя группы и результат сразу забрать в виде словаря. >>> for match in re.finditer(r"(?P<name>\w+):(?P<phone>[\d+]+)", text): >>> results.append(match.groupdict()) То есть всё что я сделал, это добавил в начале группы (внутри сбокочек) такую запись: (?P<group-name>...) Теперь найденная группа имеет имя и можно обратиться к ней как к элементу списка >>> name = match['name'] Либо забрать сразу весь словарь методом groupdict() >>> match.groupdict() #tricks#regex

Резултати

Пронајдени 95 слични објави

Пребарај: #cholera

当前筛选 #cholera清除筛选

As of 29th September, over 10,000 suspected cases of #cholera have been recorded from the 36 States + FCT. The ongoing cholera outbreak has been worsened by limited access to clean water, open defecation, & poor hygiene practices in affected communities. 🔗Download the situation report https://ncdc.gov.ng/diseases/sitreps/?cat=7&name=An%20update%20of%20Cholera%20outbreak%20in%20Nigeria

Hashtags

In reporting week 32, the number of suspected cases of #cholera increased to 121 cases. Our weekly situation report provides an update on the current #cholera epidemiological situation in Nigeria and our response activities. 🔗 Download via: https://ncdc.gov.ng/diseases/sitreps/?cat=7&name=An%20update%20of%20Cholera%20outbreak%20in%20Nigeria

Hashtags

Our weekly #cholera situation report for weeks 28 and 29 has been uploaded. The reports provide an update on the current #cholera epidemiological situation in Nigeria and our response activities. 🔗 Download via: https://ncdc.gov.ng/diseases/sitreps/?cat=7&name=An%20update%20of%20Cholera%20outbreak%20in%20Nigeria

Hashtags

Your actions, no matter how small can limit the risk of #cholera infection. 🚫Do not defecate in open areas 🧤Clear your drainages ✅Only drink water from safe sources ✅Dispose of refuse properly in waste disposal sites Public health advisory⬇️ 🔗https://ncdc.gov.ng/news/513/stop-cholera%3A-public-health-advisory

Hashtags

Our weekly #cholera situation report for week 27 has been uploaded. The report provides an update on the current #cholera epidemiological situation in Nigeria and our response activities. 🔗 Download via: https://ncdc.gov.ng/diseases/sitreps/?cat=7&name=An%20update%20of%20Cholera%20outbreak%20in%20Nigeria

Hashtags

Our #cholera situation reports are now weekly. The reports provide an update on the #cholera epidemiological situation in Nigeria and our response activities. 🔗 Download via: https://ncdc.gov.ng/diseases/sitreps/?cat=7&name=An%20update%20of%20Cholera%20outbreak%20in%20Nigeria

Hashtags

The bacteria, Vibrio cholerae, that causes #cholera is commonly found in contaminated water and food. Protect yourself. Read our public health advisory on measures to take to prevent yourself from getting infected. ⬇️ 🔗 https://ncdc.gov.ng/news/513/stop-cholera%3A-public-health-advisory

Hashtags

#Cholera Situation Report Our monthly Cholera Situation Report for May 2024 has been published on our website. 🔗You can download, read and share via https://ncdc.gov.ng/diseases/sitreps/?cat=7&name=An%20update%20of%20Cholera%20outbreak%20in%20Nigeria

Hashtags

Unwashed hands, untreated water, and uncovered food are some of the ways #Cholera germs are spread. Personal and environmental hygiene remains one of the best ways to STOP #Cholera. Watch this little boy's story on cholera: https://www.youtube.com/watch?v=jG1VNSCsP5Q

Hashtags

123•••78
ПретходнаСтраница 1 од 8Следна