Make .CSV file using MySQL and Python

In this blog, I am going to show how to develop an application by which we can make .csv files from MySQL database using Python.

Now, let us start by getting data from Database. Lets say we have a DB names csv_test and it contains a table named test with fields id and name, and for testing purpose let us have some values inserted into it like this:

| id   | name   |
|    1 | test_1 |
|    2 | test_2 |
|    3 | test_3 |

Now, to get all data from this table, the SQL command should be:

SELECT * FROM csv_test.test;

We shall run this vary command using python like this:

import subprocess

    sql_cmd = "mysql -uroot -proot -e SELECT * FROM csv_test.test;"
    results = subprocess.check_output(
            [sql_cmd], shell=True)
except Exception as e:

If we run the above command, we shell find a binary string like this:

id    name
 1    test_1
 2    test_2
 3    test_3

Actual output will be something like this:


Now we will convert this binary string to unicode string by running this command:

data = results.decode('utf-8')

And the data will be like this:


So, now we have the MySQL data as string. Now we shall start making .csv file out of it.

First, lets make an empty .csv file, ie:

filename = 'report_{0}.csv'.format('%d-%m-%Y:%H:%M'))
csv_out = open(filename, 'w')

From the data string, we will start making .csv file's contents. Let us split the string in new lines(\n) and loop through that list, also replace tabs(\t) with comma(,):

csv_data = ''
for item in data.split(`\n`):
    csv_data += '{0}\r\n'.format(item.replace('\t', ','))

Now we write that csv_data variable to csv_out(empty .csv file) like this:


Thats it, our work is done. We shall see a .csv file named REPORT_DD-MM-YYYY:HH:MM:SS.csv in the project directory.

Special Thanks to this post: about using MySQL and Flask togather

Source is here:

comments powered by Disqus