W
W
whorsash2018-05-31 18:03:32
Python
whorsash, 2018-05-31 18:03:32

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.

File link
https://yadi.sk/i/CcUXuckx3WkuoN

The primary key is the number column.
That is, the user will only enter a number.
Thanks to this number, he receives the messages name, descript, site and also a photo that will be downloaded from the link.
This can be done in order not to drive in such code:
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)

This is how you can go head-on, but I have more than 1000 numbers, are there any options?
Teach me to parse and understand this procedure, and also explain and teach with a cycle.
Yes, I googled, I didn’t really understand about parsing. You need to read about the cycle.
I'm just trying to make the bot not stop, but give out information when you re-enter, and I have to enter / start in telegram to make the bot work again. It refers to the cycle, as I understand it.
And to parse, you also need to use a loop.
Is there any solution?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Marat Nagayev, 2018-06-01
@nagayev

You can parse csv in python using the csv module (included in the standard library):
Documentation:
https://docs.python.org/3/library/csv.html

N
Nikolai Karelin, 2018-06-14
@nikolay_karelin

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 question

Ask a Question

731 491 924 answers to any question