Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #IMPORT.PY
- from cs50 import SQL
- import sys, csv
- db = SQL("sqlite:///students.db")
- def main():
- check()
- load()
- def check():
- if len(sys.argv) != 2:
- print("Usage: python import.py <file.csv>")
- def load():
- with open(sys.argv[1], 'r') as csvfile:
- csv_dict = csv.DictReader(csvfile)
- for line in csv_dict:
- insert(line["name"], line["house"], line["birth"])
- def insert(name, house, birth):
- spaces_count = name.count(' ')
- split = name.split(' ')
- first = split[0]
- if spaces_count == 2:
- middle = split[1]
- last = split[2]
- else:
- last = split[1]
- db.execute("INSERT INTO students (first, middle, last, house, birth) VALUES (?, ?, ?, ?, ?)", (first, middle, last, house, birth))
- main()
- #ROSTER.PY
- from cs50 import SQL
- import csv, sys
- db = SQL("sqlite:///students.db")
- def main():
- if len(sys.argv) != 2:
- print("Usage: python roster.py <house>")
- house = sys.argv[1].lower().capitalize()
- grab(house)
- def grab(house):
- #you can ORDER BY with more than 1 ORDER as shown:
- table = db.execute("SELECT * FROM students WHERE house = (?) ORDER BY last ASC, first ASC", house)
- for row in table:
- print(row["first"], end=' ')
- if row["middle"] != None:
- print(row["middle"], end=' ')
- print(row["last"], end=', ')
- print("born", end=' ')
- print(row["birth"])
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement