1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
#!/usr/bin/python3
import MySQLdb
import csv
import json
from glob import glob; from os.path import expanduser
web_directory="/tmp"
db = MySQLdb.connect(host="127.0.0.1",
user="root",
passwd="",
db="DBUsers", charset='utf8', use_unicode=True)
db.set_character_set('utf8')
cur = db.cursor()
cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')
sql_query = "SELECT * FROM tableUsers"
cur.execute(sql_query)
with open(web_directory + '/output.csv', "w", newline='') as csv_file:
csv_writer = csv.writer(csv_file)
csv_writer.writerows(cur)
posts = [dict(id=row[0], date=row[1], url=row[2], type=row[3], hash=row[4], country=row[5], provider=row[6]) for row in cur.fetchall()]
f = open(web_directory + '/output.json', 'w')
f.write(str(posts))
f.close() |
Partager