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

Резултати

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

Пребарај: #cartoons

当前筛选 #cartoons清除筛选
​NO Kings ​🚫👑

@No_king_No_King · Post #178 · 12.04.2026 г., 16:47

Lies... (Not friend pedophile) - Email from Melania to Ghislaine Maxwell: "Dear G, How are you? Nice story about JE in New York Mag. You look great on the picture. I know you are very busy flying all over the world. How was Palm Beach? I cannot wait to go down. Give me a call when you are back in NY. Have a great time! Love, Melania" - Melania Trump: "The lies linking me with the disgraceful Jeffrey Epstein need to end today...I've never been friends with Epstein...I never had a relationship with Epstein or his accomplice Maxwell. My email reply to Maxwell can not be categorized as anything more than casual correspondence." - "Epstein did not introduce me to Donald Trump. I met my husband by chance." - "Donald and I were invited to the same parties as Epstein from time to time." - "I call on Congress to provide the women who have been victimized by Epstein with a public hearing specifically centered around the survivors. We must work openly and transparently to uncover the truth about Epstein." Trump: "She had a right to talk about it because the fake news covers her so inaccurately. Would I have done it that way?" the president mused. "Perhaps not, perhaps, I don't know." @michael_de_adder@rickmckeetoons davegranlundcartoons @mjhiblenart@lowtekguy@tooner13@jackohman60@misterjesseduquette@cdurando49@repeat1968#JeffDanziger@billdaycartoons@billbramhallcartoons@[email protected] #Cartoons#CoffeeAndCourtney#ComicRelief#CartoonCurator ✈️t.me/No_king_No_King

12
ПретходнаСтраница 1 од 2Следна