-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdata_import_from_csv.py
executable file
·42 lines (34 loc) · 1.28 KB
/
data_import_from_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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
#==============================================================================
# This script imports data into the database from CSV Files
#==============================================================================
import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "subtitleStatus.settings")
import django
django.setup()
from django.core.management.base import BaseCommand, CommandError
from django.db import transaction
from django.core.exceptions import ObjectDoesNotExist
from www.models import Talk
if len(sys.argv) < 2:
sys.exit("Not enough arguments! Filename needed!")
read_file = open(sys.argv[1], mode = "r", encoding = "utf-8")
file_content = read_file.read()
text_content = file_content.splitlines()
text_content_separated=[]
for line in text_content:
new_line = line.split(';')
text_content_separated.append(new_line)
for line in text_content_separated:
print(len(line),line)
try:
my_talk = Talk.objects.get(frab_id_talk = int(line[0]))
my_talk.amara_key = line[2]
my_talk.youtube_key = line[3]
my_talk.video_duration = line[4]
my_talk.link_to_video_file = line[6]
my_talk.save()
except ObjectDoesNotExist:
print(line[0]+" gibts nicht!")