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

Пребарај: #obt

当前筛选 #obt清除筛选
Nothing Phone (2) | Updates

@NothingPhone2Updates · Post #468 · 05.11.2024 г., 16:11

Nothing OS 3.0 (A15) | OBT-1 for Phone (2) is here! ❗️Disclaimer 🔽 Downloads: Beta Updater (over Pong-U2.6-241016-1700) | Rollback Package (Instructions) ✍️Installation | To-Do List (Read before proceeding) ⚙️Changelogs 🔔 Updates : @NothingPhone2Updates 💬 Chat : @NothingPhone2 #Pong#OBT#A15

Nothing Phone (2) | Updates

@NothingPhone2Updates · Post #184 · 09.11.2023 г., 12:05

Nothing OS 2.5 Open Beta 2 | Phone (2) | Android 14 📝 Details: - Version: 2.5 Beta 2 - Type: #NOS#OBT#A14#Beta - Codename: #Pong - Released: 02/11/23 Links: ⬇️ Download : Beta tool | Beta Package (Full) | Incremental (from beta 1) | Rollback (to 2.0.4) | TG Community Archive ⚙️ Changelogs | Official Intro 🏛Nothing Community Website 📸 Screenshots 🗒To-Do List Disclaimer: - Keep checking system updates (if you are already on beta 1). - Since this is a Beta version, there’s a very small chance the test software may interfere with your data and require a factory reset. In this instance, your data will be lost. We strongly recommend backing up all your data before accessing the software. - Rollback to NOS 2.0.4 Involves data loss, backup prior proceeding - For those getting error "ENOENT" during Android 14 Beta update: Make sure you have a folder named Documents in your storage 🔔 Updates : @NothingPhone2Updates 🐙 Chat : @NothingPhone2

Nothing Phone (2) | Updates

@NothingPhone2Updates · Post #173 · 11.10.2023 г., 12:26

Nothing OS 2.5 Open Beta 1 | Phone (2) | Android 14 📝 Details: - Version: 2.5 Beta 1 - Type: #NOS#OBT#A14#Beta - Codename: #Pong - Released: 07/10/23 Links: ⬇️ Download : Beta tool | Beta Package | Rollback | Gdrive 🏛Nothing Community Website (Installation & Changelogs) 📸 Screenshots 🗒To-Do List Disclaimer: - Since this is a Beta version, there’s a very small chance the test software may interfere with your data and require a factory reset. In this instance, your data will be lost. We strongly recommend backing up all your data before accessing the software. - Rollback to NOS 2.0.3 Involves data loss, backup prior proceeding - For those getting error "ENOENT" during Android 14 Beta update: Make sure you have a folder named Documents in your storage 🔔 Updates : @NothingPhone2Updates 🐙 Chat : @NothingPhone2