Created
July 28, 2015 15:10
-
-
Save lepture/368d9c58f11f30830949 to your computer and use it in GitHub Desktop.
export june database to JSON
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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