Files
chinese-poetry/ci/main.py
cclauss 616c2b04a3 reload() was moved into importlib in Python 3
The builtin __reload()__ was moved into [importlib](https://docs.python.org/3/library/importlib.html#importlib.reload) in Python 3 because it was being overused and led to odd behavior that was often difficult to diagnose.  Here we are trying to __reload(sys)__ probably because 'utf-8' encoding and Unicode strs are not the default in Python 2.  They are however the default in Python 3 so this PR therefor suggests that a __pass__ would be satisfactory in Python 3.
2018-03-02 10:48:48 +01:00

47 lines
1.1 KiB
Python

#!-*- coding: utf-8 -*-
import json, sys
import sqlite3
from collections import OrderedDict
try: # Python 2
reload(sys)
sys.setdefaultencoding('utf-8')
except NameError: # Python 3
pass
c = sqlite3.connect('ci.db')
cursor = c.execute("SELECT name, long_desc, short_desc from ciauthor;")
d = {"name": None, "description": None, "short_description": None}
authors = []
for row in cursor:
author = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
author["name"] = row[0]
author["description"] = row[1]
author["short_description"] = row[2]
authors.append(author)
open('author.song.json', 'w').write(json.dumps(authors, indent=2, ensure_ascii=False))
cursor = c.execute("SELECT rhythmic, author, content from ci;")
d = {"rhythmic": None, "author": None, "paragraphs": None}
cis = []
for row in cursor:
ci = OrderedDict(sorted(d.items(), key=lambda t: t[0]))
ci["rhythmic"] = row[0]
ci["author"] = row[1]
ci["paragraphs"] = row[2].split('\n')
cis.append(ci)
for i in range(0, 21050, 1000):
open('ci.song.%s.json' % i, 'w').write(json.dumps(cis[i:i+1000], indent=2, ensure_ascii=False))