-
Notifications
You must be signed in to change notification settings - Fork 2
/
problem_set7bi.py
57 lines (41 loc) · 1.47 KB
/
problem_set7bi.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
47
48
49
50
51
52
53
54
55
56
57
from sys import argv
import csv
import cs50
def main():
# check for correct number of arguments
if len(argv) != 2:
print("Usage: import.py file.csv")
return 1
# open database
db = cs50.SQL("sqlite:///students.db")
# open csv file from argv
with open(argv[1], "r") as csv_file:
# create dictreader
reader = csv.DictReader(csv_file)
# loop through rows of csv file and add info to table in database
for row in reader:
names = extract_names(row["name"])
if len(names) == 2:
db.execute("INSERT INTO students (first, last, house, birth) VALUES(?, ?, ?, ?)",
names[0], names[1], row["house"], row["birth"])
else:
db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)",
names[0], names[1], names[2], row["house"], row["birth"])
# given fullname, extract individual names
def extract_names(fullname):
# initialise variables
names = []
single_name = ''
index = 0
# loop through name string and add individual names to names array
for c in fullname:
if c == ' ':
names.append(single_name)
single_name = ''
index += 1
else:
single_name += c
# at the end of for loop, final name from fullname won't yet be added to names array
names.append(single_name)
return names
main()