PYTHONL Telegram 5122
🐍 Простые фишки парсинга в Python

1️⃣ Парсинг больших JSON-файлов без загрузки в память

import ijson

with open("big.json", "r") as f:
for item in ijson.items(f, "records.item"):
print(item) # потоковый парсинг, не держим всё в памяти


2️⃣ HTML-парсинг с поддержкой XPath через lxml


from lxml import html

doc = html.fromstring("<div><span>Hello</span></div>")
print(doc.xpath("//span/text()")[0]) # Hello


3️⃣ Парсинг логов с регулярками и именованными группами


import re

line = '2025-08-23 12:10:45 [INFO] User=egor Action=login'
pattern = r'(?P<date>\d{4}-\d{2}-\d{2}) .* User=(?P<user>\w+) Action=(?P<action>\w+)'
m = re.search(pattern, line)
print(m.groupdict())
# {'date': '2025-08-23', 'user': 'egor', 'action': 'login'}


4️⃣ Парсинг YAML c поддержкой типов


import yaml

data = yaml.safe_load("""
user: egor
active: true
age: 30
""")
print(data) # {'user': 'egor', 'active': True, 'age': 30}

5️⃣ Парсинг бинарных данных (struct)



import struct

raw = b"\x01\x00\x00\x00\x2A\x00"
id, value = struct.unpack("<iH", raw)
print(id, value) # 1 42

6️⃣ Парсинг HTML-таблиц напрямую в DataFrame (pandas)



import pandas as pd

url = "https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)"
tables = pd.read_html(url)
print(tables[0].head()) # первая таблица со страницы


🔥 Эти методы позволяют эффективно парсить большие JSON, бинарные форматы, HTML с XPath, YAML и даже таблицы прямо в pandas.
Используйте их, если обычных инструментов уже не хватает.
🔥1911👍7



tgoop.com/pythonl/5122
Create:
Last Update:

🐍 Простые фишки парсинга в Python

1️⃣ Парсинг больших JSON-файлов без загрузки в память


import ijson

with open("big.json", "r") as f:
for item in ijson.items(f, "records.item"):
print(item) # потоковый парсинг, не держим всё в памяти


2️⃣ HTML-парсинг с поддержкой XPath через lxml


from lxml import html

doc = html.fromstring("<div><span>Hello</span></div>")
print(doc.xpath("//span/text()")[0]) # Hello


3️⃣ Парсинг логов с регулярками и именованными группами


import re

line = '2025-08-23 12:10:45 [INFO] User=egor Action=login'
pattern = r'(?P<date>\d{4}-\d{2}-\d{2}) .* User=(?P<user>\w+) Action=(?P<action>\w+)'
m = re.search(pattern, line)
print(m.groupdict())
# {'date': '2025-08-23', 'user': 'egor', 'action': 'login'}


4️⃣ Парсинг YAML c поддержкой типов


import yaml

data = yaml.safe_load("""
user: egor
active: true
age: 30
""")
print(data) # {'user': 'egor', 'active': True, 'age': 30}

5️⃣ Парсинг бинарных данных (struct)



import struct

raw = b"\x01\x00\x00\x00\x2A\x00"
id, value = struct.unpack("<iH", raw)
print(id, value) # 1 42

6️⃣ Парсинг HTML-таблиц напрямую в DataFrame (pandas)



import pandas as pd

url = "https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)"
tables = pd.read_html(url)
print(tables[0].head()) # первая таблица со страницы


🔥 Эти методы позволяют эффективно парсить большие JSON, бинарные форматы, HTML с XPath, YAML и даже таблицы прямо в pandas.
Используйте их, если обычных инструментов уже не хватает.

BY Python/ django


Share with your friend now:
tgoop.com/pythonl/5122

View MORE
Open in Telegram


Telegram News

Date: |

Hashtags Healing through screaming therapy best-secure-messaging-apps-shutterstock-1892950018.jpg Image: Telegram. Members can post their voice notes of themselves screaming. Interestingly, the group doesn’t allow to post anything else which might lead to an instant ban. As of now, there are more than 330 members in the group.
from us


Telegram Python/ django
FROM American