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

Пребарај: #modelsai

当前筛选 #modelsai清除筛选
Repositorio data science

@repo_science · Post #3240 · 29.05.2023 г., 15:22

#ChatGPT#modelsAI#freelancer 🤩 Make Money Freelancing with Chatpgpt - Step-by-Step Are you ready to take your freelancing business to the next level with the power of AI? Look no further than “Freelancing Business with AI & Chatgpt – Learn Step-by-step” – the comprehensive course designed to help you succeed in the world of online freelancing. In this course, you’ll learn proven step-by-step strategies for building a profitable freelancing business that harnesses the latest AI technologies. 🔗Link 🔐@repo_science ----- Main channel:@repo_science Coupons:@freecoupons_reposcience -----

Repositorio data science

@repo_science · Post #3241 · 29.05.2023 г., 15:54

#ChatGPT#modelsAI#freelancer#wordPress 🤩 Start Building Money Making Blog with ChatGPT & WordPress Are you interested in starting a blog but don’t know where to begin? Do you want to earn money while doing something you love? Look no further than WordPress, the most popular platform for creating and managing a blog. In this comprehensive course, we’ll guide you through the process of setting up a WordPress blog and show you how to turn it into a money-making machine. Our course, “Start Free Money Making Blogging with ChatGPT & WordPress,” is designed for beginners who want to learn the ins and outs of blogging with WordPress. With over 40% of all websites on the internet using WordPress, it’s clear that this platform is the best choice for creating a successful blog. 🔗Link ----- Main channel:@repo_science Coupons:@freecoupons_reposcience -----