Answer the question
In order to leave comments, you need to log in
How to parse CSV correctly?
Hello. I am writing a bot that would help the user get the necessary information for a given number.
Here I have a CSV file.
Too bad I can't embed the file.
import telebot
from telebot import types
token = 'Здеся мой токен'
bot= telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def start(m):
msg=bot.send_message(m.chat.id,
'''Вас приветствует whorsash''')
logotip = open('C:\\Users\\user\\logo.png', 'rb')
bot.send_photo(m.chat.id, logotip)
messg=bot.send_message(m.chat.id,
'''Для поиска информации введите номер''')
bot.register_next_step_handler(messg, name)
def name(t):
#101
if t.text == '101':
bot.send_message(t.chat.id,
'''Тачка''')
n101 = open('http://arshin48.ru/upload/iblock/717/sad.tachka-nv-1302.jpg', 'rb')
bot.send_photo(t.chat.id, n101)
bot.send_message(t.chat.id,
'''Тачка садовая, элегантная''')
bot.send_message(t.chat.id,
'''http://arshin48.ru/catalog/27657/915202/''')
#301
elif t.text == '301':
bot.send_message(t.chat.id,
'''Бабки''')
n301 = open('http://g4.nh.ee/images/pix/1000x654/yEP_eSREsiY/6cce0564d8f9c9b73f-72993621.jpg', 'rb')
bot.send_photo(t.chat.id, n301)
bot.send_message(t.chat.id,
'''2 билета на концерт новых русских бабок''')
bot.send_message(t.chat.id,
'''http://mail.ru''')
else:
bot.send_message(t.chat.id,
'''Упс... Такого номера у нас нет...
повторите ввод''')
bot.polling(none_stop=True, interval=0)
Answer the question
In order to leave comments, you need to log in
You can parse csv in python using the csv module (included in the standard library):
Documentation:
https://docs.python.org/3/library/csv.html
1000 lines is not much, I would read it into memory when the server starts, save it to a dictionary and then scoop it out according to the code in the request. Following the example in the csv documentation, the file is read by the module of the same name in 3-5 lines.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question