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

Резултати

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

Пребарај: #pappe

当前筛选 #pappe清除筛选
MessageInABottle

@mib_messageinabottle · Post #6804 · 17.05.2024 г., 10:24

🇺🇸#USA 🇮🇱#Israel #ISRAELI ANTI-#ZIONIST HISTORIAN ILAN #PAPPE INTERROGATED BY #FBI AT DETROIT #AIRPORT The professor said that he was interrogated about his personal connections, with the agents inquiring about his #Arab and #Muslim friends in the USA, the length of their relationships, and the nature of their interactions. Pappe responded by directing the agents to his published works in some instances, while providing brief “yes” or “no” answers in others, citing exhaustion after an eight-hour flight. He also mentioned that the #FBI agents engaged in a lengthy phone conversation with an unidentified party, which he speculated could have been an Israeli official. Following the conversation, the agents copied the contents of his phone before allowing him to enter the country. https://x.com/Resist_05/status/1791001769591627894 https://www.middleeastmonitor.com/20240516-us-ilan-pappe-interrogated-by-fbi-at-detroit-airport/