Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # -*- coding: utf-8 -*-
- import mysql.connector
- import csv
- from os import getenv
- def mysql_connect():
- return mysql.connector.connect(
- user=getenv('USER'), password=getenv('PASSWORD'),
- host=getenv('HOST'), database=getenv('DATABASE')
- )
- def main():
- cnx = mysql_connect()
- cursor = cnx.cursor()
- query_file = getenv('QUERY_FILE')
- with open(query_file, 'r') as qf:
- query_file_contents = qf.read()
- cursor.execute(query_file_contents)
- with open('output.csv', 'wb') as csvfile:
- filewriter = csv.writer(
- csvfile, delimiter=',',
- quotechar='"', quoting=csv.QUOTE_MINIMAL
- )
- for line in cursor:
- newline = []
- for x in line:
- typex = type(x)
- newx = x.encode('utf8') if typex is str \
- else x
- newline.append(newx)
- filewriter.writerow(newline)
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement