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

Резултати

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

Пребарај: #venezuelan

当前筛选 #venezuelan清除筛选
Addis Standard

@addisstandardeng · Post #21033 · 10.01.2026 г., 09:15

#Pope_Leo says ‘war is back in vogue’ in major address to global envoys Pope Leo XIV has said the prohibition on countries violating one another’s borders has been “completely undermined,” in an important speech to diplomats gathered in the #Vatican. The first #United_States-born pope, giving his debut “state of the world” address, expressed his concern about “escalating tensions” in the “#Caribbean Sea and the #American Pacific coast” while calling for the “will of the #Venezuelan people” to be respected, and a return to stability in the country. “War is back in vogue and a zeal for war is spreading,” Leo told ambassadors from across the globe gathered in the Vatican’s Apostolic Palace on Friday. “The principle established after the Second World War, which prohibited nations from using force to violate the borders of others, has been completely undermined.” Leo was speaking just less than a week after the...... https://edition.cnn.com/2026/01/09/europe/pope-leo-state-of-world-address-envoys-intl