get_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
44
45
# -*- coding:utf-8 -*-
import codecs
import datetime
import os
from django.core.management.base import BaseCommand
from settings import PROJECT_PATH
from webapp.models import Meaning
CSV_PATH = os.path.join(PROJECT_PATH, 'data', 'periphraser_%s.csv' % datetime.datetime.now().strftime('%Y%m%d'))
LABELS = [u'znaczenie',
u'wyrażenie',
u'wyrażenie pochodne',
u'źródło',
u'kategoria',
u'wikilink']
class Command(BaseCommand):
help = 'Get database as csv.'
def handle(self, *args, **options):
write_csv()
def write_csv():
try:
csv_file = codecs.open(CSV_PATH, 'wt', 'utf-8')
csv_file.write(u'%s\n' % u'\t'.join(LABELS))
for meaning in Meaning.objects.order_by('id'):
print meaning.id
for expr in meaning.expressions.filter(main_expression=None).order_by('text'):
main_mention = ''
if expr.mentions.exists():
main_mention = expr.mentions.all()[0].text
csv_file.write(u'%d\t%s\t%s\t%s\t%s\t%s\n' % (meaning.id,
expr.text,
main_mention,
expr.sources.all()[0].name,
meaning.category.name,
meaning.wikilink))
finally:
csv_file.close()