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

Резултати

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

Пребарај: #bookfair

当前筛选 #bookfair清除筛选
Venezuelanalysis

@venanalysis · Post #2026 · 09.07.2025 г., 19:42

📸 IN IMAGES | Venezuela Challenges Eurocentric Narratives in International Book Fair President Nicolás Maduro inaugurated the 21st edition of the Venezuelan International Book Fair (FILVEN), announcing a new initiative to distribute printed books in schools nationwide. The event, held in Caracas, features Egypt as the guest of honor, commemorating 75 years of diplomatic relations. With 18 publishers from 10 countries, FILVEN runs through July 13 before continuing across other Venezuelan states. Follow the link to check out our gallery: https://venezuelanalysis.com/images/venezuela-challenges-eurocentric-narratives-in-international-book-fair/ #BookFair#Culture

Venezuelanalysis

@venanalysis · Post #1659 · 22.07.2024 г., 22:21

Venezuela Honors South Africa at the 20th International Book Fair At Venezuela’s 20th International Book Fair, South Africa was honored with 18 authors, and Nelson Mandela’s legacy was celebrated. The event also featured Jorge Ramos Tolosa’s book on Palestine. Don’t miss out on the gallery by clicking here 👉🏼https://shorturl.at/PSCso #bookfair#filven#filven2024#venezuela

Green University CA | Official

@centralasian_greenuniversity · Post #601 · 27.12.2025 г., 13:02

📚Book Fair at Green University 📖A book fair was held at the university, bringing together students, faculty, and book lovers in a lively academic atmosphere. The event offered a wide selection of literature, encouraged reading culture, and created a space for meaningful exchange of ideas. #BookFair#ReadingCulture#StudentLife#LoveForBooks ✉️[email protected] ☎️ +998 55 512 00 77 📍Location 🌐Website | 📱Telegram | 📱Instagram | 📱LinkedIn | 🟥YouTube