How to create a csv file in memory and send it with Telegram bot with Python.

Dan Okhlopkov
1 min readApr 26, 2020

--

How to create a csv file in memory (without saving the data on disk) and send it via Telegram bot with Python. How to send a file with Telegram bot without saving it on disk.

Problem

Imagine that you need to create an automatic report in CSV format. Imagine that you are a backend developer that doesn’t have frontend skills but still you want to code the report autogeneration. Imagine that you have some tabular data that your teammates (non-coders) want to export from your service or just database.

My solution

Create a Telegram Messenger bot that will generate CSV files with the required data. I’ll show you how I did that in Python.

My key points:

  • Actually I don’t want to create a CSV file and save in on disk
  • I want to create CSV file in memory and
  • Send the file using python-telegram-bot library.

To create a CSV file I used the standard library ‘csv’, for buffer memory management I used standard library ‘io’. Enjoy

The snippet with comments

I spent a couple of hours on googling each line from my final solution so I decided to share it with you. All this buffer in-memory stuff is hard but really cool.

Please follow me for more hacks! My sexy website: okhlopkov.com

--

--

Responses (1)