Skip to content

Instantly share code, notes, and snippets.

@elena-roff
Last active July 24, 2018 09:49
Show Gist options
  • Save elena-roff/c278fd3ee2bd419b65202c8d97fb58c1 to your computer and use it in GitHub Desktop.
Save elena-roff/c278fd3ee2bd419b65202c8d97fb58c1 to your computer and use it in GitHub Desktop.
Working with data: Useful code chunks
#!/usr/bin/env python3
""" Script to fetch the data from the server and store in a csv file.
ARGS:
- SQL query to parse.
- filename to store the data to in the csv format.
OUTPUT:
- csv file with the data."""
import pandas as pd
# for mysql DB
from mysql.connector import pooling
import os
import sys
if (len(sys.argv) != 3):
raise Exception('Two arguments should be provided: 1 - query.sql, 2 - filename.csv.')
QUERYNAME = sys.argv[1]
FILEOUTPUT = sys.argv[2]
conf = {}
conf['user'] = 'your_user'
conf['host'] = 'your_host'
conf['database'] = 'your_db_name'
conf['port'] = 'your_port'
conf['password'] = 'your_pass'
cnx = pooling.MySQLConnectionPool(**conf)
with open(QUERYNAME) as fh:
query = fh.read()
data = pd.read_sql(query, cnx.get_connection())
data.to_csv(FILEOUTPUT, index = False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment