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: |

Commenting about the court's concerns about the spread of false information related to the elections, Minister Fachin noted Brazil is "facing circumstances that could put Brazil's democracy at risk." During the meeting, the information technology secretary at the TSE, Julio Valente, put forward a list of requests the court believes will disinformation. The Standard Channel A vandalised bank during the 2019 protest. File photo: May James/HKFP. ‘Ban’ on Telegram While the character limit is 255, try to fit into 200 characters. This way, users will be able to take in your text fast and efficiently. Reveal the essence of your channel and provide contact information. For example, you can add a bot name, link to your pricing plans, etc.
from us


Telegram Python/ django
FROM American