-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate_csv.py
46 lines (39 loc) · 1.4 KB
/
create_csv.py
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
46
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sukeroom.settings
import csv
import re
os.environ['DJANGO_SETTINGS_MODULE'] = 'sukeroom.settings'
from rooms.models import Company, Question, Answer
def sub_comma(arg):
if arg.find(',') != -1:
return re.sub(',', '', arg)
return arg
def create_csv():
data_writer = csv.writer(open('eggs.csv', 'wb'),
quotechar='|', quoting=csv.QUOTE_NONE)
data_writer.writerow(['Spam'] * 5 + ['Baked Beans'])
companies = Company.objects.all()
questions = Question.objects.all().order_by('id')
# first line, questions
qs = [u'会社名']
for question in questions:
qs.append(sub_comma(question.question_sentence))
data_writer.writerow([s.encode("utf-8") for s in qs])
# after second line, answers
for company in companies:
answers = [sub_comma(company.company_name)]
ans_list = Answer.objects.filter(
company=company).order_by('question__id')
ans_idx = 0
for q in questions:
if ans_idx < len(ans_list) and q.id == ans_list[ans_idx].question.id:
ans = ans_list[ans_idx]
ans_idx += 1
else:
ans = Answer(answer=u'未回答')
answers.append(sub_comma(ans.answer))
data_writer.writerow([s.encode("utf-8") for s in answers])
if __name__ == '__main__':
create_csv()