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

Пребарај: #socai

当前筛选 #socai清除筛选
SpotOnChain | Announcement

@spotonchain · Post #965 · 14.08.2024 г., 07:44

🎁🎁 Unlock Big Rewards with Spot On Sentiment, the first on-chain game on Telegram! Introducing Spot On Sentiment, the game where you share on-chain insights and earn rewards with just a swipe. 👉 Free to play 👉 Earn points and seize the $SOCAI airdrop 👉 Get real-time updates on on-chain signals 🎮 Play now and seize the rewards: https://t.me/socai_bot/sentiment?startapp=9b48fc9c0cb2 #spotonchain#SOCAI

SpotOnChain | Announcement

@spotonchain · Post #836 · 27.05.2024 г., 09:00

🎉 Spotonchain is thrilled to announce our Proof of Sentiment and the latest updates! This revolutionary concept combines emotion with data analysis for smarter trading. The $SOCAI token fuels our system, rewarding accurate sentiment and enabling successful trades. Learn more at https://docs.spotonchain.ai/proof-of-sentiment/introduction We've also upgraded our referral program and airdrop campaign: 👉 Earn 10% of the points your friends accumulate. 👉 Track your contributions on the airdrop page https://platform.spotonchain.ai/en/account?route=airdrop Stay tuned for more exciting updates—happy point farming! #SOCAI#ProofOfSentiment

SpotOnChain | Announcement

@spotonchain · Post #1062 · 31.12.2024 г., 15:34

🎉 Hello, 2025, from SpotOnChain! Thank you for an incredible 2024. Big things are coming this year, including the launch of our AI-powered trading agent and the highly anticipated $SOCAI token. 🚀 Here’s to redefining your crypto journey together in 2025! 🥂 #OnChain#SpotOnChain#SOCAI