Last active
January 4, 2021 17:44
-
-
Save Hamleyburger/8491dd8f8b3e826491310f64f422f10b to your computer and use it in GitHub Desktop.
Solution for pset7 houses, CS50 2020
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
import csv | |
import sys | |
def main(): | |
# checking argv (that there's one and it's a csv) | |
if (len(sys.argv) != 2): | |
sys.exit("Usage: import.py file.csv") | |
filename = sys.argv[1] | |
if not (filename.endswith(".csv")): | |
sys.exit("You must provide a *.csv") | |
# Connect with the .db file and make a cursor | |
sqlite_file = "students.db" | |
con = sqlite3.connect(sqlite_file) | |
cur = con.cursor() | |
# Open the csv file to import from | |
with open(filename, "r") as characters: | |
# Make a dictionary reader that iterates through rows | |
reader = csv.DictReader(characters) | |
for row in reader: | |
names = [] | |
for part in row["name"].split(" "): | |
names.append(part) | |
names.append(row["house"]) | |
names.append(row["birth"]) | |
if (len(names) == 5): | |
cur.execute("INSERT INTO students (first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)", names[:5]) | |
if (len(names) == 4): | |
cur.execute("INSERT INTO students (first, last, house, birth) VALUES(?, ?, ?, ?)", names[:4]) | |
con.commit() | |
con.close() | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
import csv | |
import sys | |
def main(): | |
# checking argv (needs to be one, house name) | |
if (len(sys.argv) != 2): | |
sys.exit("Usage: roster.py house_name") | |
# Make house name lower to avoid cap sensitivity | |
housename = sys.argv[1].lower() | |
# Check if argument is indeed a house at Hogwarts | |
houses = ["slytherin", "gryffindor", "ravenclaw", "hufflepuff"] | |
if housename.lower() not in houses: | |
sys.exit("provide house name: Gryffindor, Hufflepuff, Slytherin or Ravenclaw.") | |
# Connect with the .db file and make a cursor | |
sqlite_file = "students.db" | |
con = sqlite3.connect(sqlite_file) | |
cur = con.cursor() | |
cur.execute('SELECT first, middle, last, birth FROM students WHERE lower(house) = "{}" ORDER BY last, first;'.format(housename)) | |
# Fetchall gives us all the rows of the table as a list of tuples with strings. | |
houseroster = cur.fetchall() | |
# Do stuff with each row in table | |
for row in houseroster: | |
if not row[1]: | |
print("{} {}, born {}".format(row[0], row[2], row[3])) | |
else: | |
print("{} {} {}, born {}".format(row[0], row[1], row[2], row[3])) | |
con.close() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pretty neat coding btw 👍