-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_sonnets.py
More file actions
29 lines (21 loc) · 860 Bytes
/
Copy pathload_sonnets.py
File metadata and controls
29 lines (21 loc) · 860 Bytes
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
from django.core.management.base import BaseCommand
from sonnets.models import Sonnet
class Command(BaseCommand):
def load_sonnets(self):
# Open file containing sonnets
with open('1041.txt') as f:
lines = f.readlines()
f.close()
# Isolate lines containing sonnets
sonnet_lines = [line.strip() for line in lines[43:2662]]
# Group sonnets in list of lists
sonnets = []
for i in range(0, len(sonnet_lines), 17):
sonnets += [sonnet_lines[i:i+17]]
# Remove all empty strings
sonnets = [[line for line in sonnet if line] for sonnet in sonnets[:-1]]
for sonnet in sonnets:
sonnet = Sonnet(number=sonnet[0], text=sonnet[1:])
sonnet.save()
def handle(self, *args, **options):
self.load_sonnets()