📦 Урок 8: CSV → Dict → JSON + бот
💡 Дані проходять шлях: файл → структура Python → файл знову, але вже як JSON.
🔹 CSV файл (табличні дані)
id,name,score
1,Alex,10
2,Maria,5
3,Ivan,7
✔ CSV — це таблиця у текстовому вигляді
🔹 Dict у Python
users = [
{"id": 1, "name": "Alex", "score": 10},
{"id": 2, "name": "Maria", "score": 5},
{"id": 3, "name": "Ivan", "score": 7}
]
✔ Dict = структура даних у Python
🔹 JSON файл (збереження даних)
[
{"name": "Alex", "score": 10},
{"name": "Maria", "score": 5},
{"name": "Ivan", "score": 7}
]
✔ JSON = файл для збереження структурованих даних
🤖 Бот (читання JSON + /start)
import json
from bendbandbot import SimpleBot
with open("token.key", "r", encoding="utf-8") as f:
token = f.read().strip()
bot = SimpleBot(token)
with open("users.json", "r", encoding="utf-8") as f:
users = json.load(f)
def start(update):
msg = "👥 Список користувачів:\n\n"
for u in users:
msg += "👤 " + u["name"] + " — ⭐ " + str(u["score"]) + "\n"
bot.send(update, msg)
bot.on_command("start", start)
bot.start()
🧪 Завдання
✍️ Додай 2 нових користувачів у JSON
✍️ Зміни score існуючих
✍️ Перевір /start