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 слични објави

Пребарај: #yeshiwas

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

@addisstandardeng · Post #21371 · 11.02.2026 г., 14:05

News: #EPRP says politician #Yeshiwas Assefa arrested, calls for immediate release The #Ethiopian People’s Revolutionary Party (EPRP) has announced that politician Yeshiwas Assefa has been arrested, saying it confirmed the development through his family members. In a statement issued today, the party said Yeshiwas was taken into custody yesterday, describing the arrest as part of what it called the government’s continued crackdown on citizens engaged in peaceful political activity. “This is one of the countless acts that demonstrate the government’s ongoing suppression of citizens engaging in peaceful political activities,” the statement read. The EPRP further argued that such actions cast doubt on the credibility of the upcoming elections, which ruling party officials have repeatedly pledged will be free, fair and democratic. Instead, the party said, the current https://web.facebook.com/AddisstandardEng/posts/pfbid0haYBwZFd8QeABk6i1v9kN4LtHyVXkPGG4mFcjx4T6UPUAx1SVkJ5PCi2FpXQ74Col

Addis Standard

@addisstandardeng · Post #21376 · 12.02.2026 г., 06:36

#Ethiopia: Politician #Yeshiwas Assefa released hours after arrest Politician Yeshiwas Assefa has been released from custody hours after his arrest was confirmed by his party, the Ethiopian People’s Revolutionary Party (#EPRP). Mistresellassie Tamrat, General Secretary of EPRP, confirmed to Addis Standard that Yeshiwas was released yesterday at approximately 5:00 PM. She said the politician, who is also an EPRP election candidate, was freed after being detained yesterday. Earlier in the day, EPRP said it had verified through family members that Yeshiwas had been arrested. In a statement, the party described the arrest as “one of the countless acts demonstrating the government's ongoing suppression of citizens engaged in peaceful political activities.” Yeshiwas, who previously served as chairman of the Ethiopian Citizens for Social Justice (#EZEMA), has faced prior.... https://web.facebook.com/AddisstandardEng/posts/pfbid0joxCZicZF8EgEDFQmHdS2DJTZw56NdhZHg3pkF7p2bwbMKPTLtWMyroD9duASBtvl