Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import mysql.connector
  4. import csv
  5. from os import getenv
  6.  
  7. def mysql_connect():
  8. return mysql.connector.connect(
  9. user=getenv('USER'), password=getenv('PASSWORD'),
  10. host=getenv('HOST'), database=getenv('DATABASE')
  11. )
  12.  
  13. def main():
  14.  
  15. cnx = mysql_connect()
  16. cursor = cnx.cursor()
  17.  
  18. query_file = getenv('QUERY_FILE')
  19. with open(query_file, 'r') as qf:
  20. query_file_contents = qf.read()
  21.  
  22. cursor.execute(query_file_contents)
  23.  
  24. with open('output.csv', 'wb') as csvfile:
  25.  
  26. filewriter = csv.writer(
  27. csvfile, delimiter=',',
  28. quotechar='"', quoting=csv.QUOTE_MINIMAL
  29. )
  30.  
  31. for line in cursor:
  32. newline = []
  33. for x in line:
  34. typex = type(x)
  35. newx = x.encode('utf8') if typex is str \
  36. else x
  37. newline.append(newx)
  38.  
  39. filewriter.writerow(newline)
  40.  
  41.  
  42. if __name__ == '__main__':
  43. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement