export_as_csv.py
1.51 KB
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
38
39
40
41
42
43
# -*- coding:utf-8 -*-
import codecs
import datetime
import os
from django.core.management.base import BaseCommand
from normalization import normalize
from settings import PROJECT_PATH
from webapp.models import Meaning
NORMALIZE = True
SOURCE = 'sjp'
CSV_PATH = os.path.join(PROJECT_PATH, 'data', '%s_%s.csv' % (SOURCE,
datetime.datetime.now().strftime('%Y%m%d')))
class Command(BaseCommand):
help = 'Get database as csv.'
def handle(self, *args, **options):
export_csv()
def export_csv():
try:
csv_file = codecs.open(CSV_PATH, 'wt', 'utf-8')
meanings = Meaning.objects.filter(expressions__link__source__key=SOURCE).distinct()
for meaning in meanings.order_by('id'):
print meaning.id
if NORMALIZE:
mng_expressions = meaning.expressions.filter(main_expression=None)
orth_expressions = [expr.orth_text for expr in mng_expressions]
orth_expressions.extend(normalize.expressions(mng_expressions))
csv_file.write(u'%d\t%s\n' % (meaning.id,
u'\t'.join([expr for expr in orth_expressions])))
else:
csv_file.write(u'%d\t%s\n' % (meaning.id,
u'\t'.join([expr.orth_text for expr in
meaning.expressions.filter(main_expression=None)])))
finally:
csv_file.close()