Skip to content

Instantly share code, notes, and snippets.

@lepture
Created July 28, 2015 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lepture/368d9c58f11f30830949 to your computer and use it in GitHub Desktop.
Save lepture/368d9c58f11f30830949 to your computer and use it in GitHub Desktop.
export june database to JSON
# code: utf-8
import json
import datetime
import pymysql
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, datetime.datetime):
return str(o)
return json.JSONEncoder.default(self, o)
def dumps(obj):
return json.dumps(obj, cls=JSONEncoder)
conn = pymysql.connect(user='root', db='june', charset='utf8')
def fetch(name):
cursor = conn.cursor()
cursor.execute('select * from %s' % name)
column_names = [d[0] for d in cursor.description]
for row in cursor:
yield dict(zip(column_names, row))
rv = dict(
users=list(fetch('account')),
nodes=list(fetch('node')),
nodes_status=list(fetch('node_status')),
topics=list(fetch('topic')),
liked_topics=list(fetch('like_topic')),
comments=list(fetch('reply')),
)
with open('june.json', 'w') as f:
json.dump(rv, f, cls=JSONEncoder)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment