Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. from csv import reader
  2. from csv import DictReader
  3. from sys import argv
  4. import csv
  5. from cs50 import SQL
  6. import cs50
  7.  
  8. # Give access to students.db
  9.  
  10. db=SQL("sqlite:///students.db")
  11.  
  12. def main():
  13.  
  14. if len (argv) != 2:
  15. print("Error: Argv Length")
  16. exit(1)
  17.  
  18. with open(argv[1],"r") as chars:
  19. reader = csv.DictReader(chars)
  20.  
  21. id_count = 0
  22. for row in reader:
  23. splitname = row["name"].split()
  24. if len(splitname) == 3:
  25. first = splitname[0]
  26. middle = splitname[1]
  27. last = splitname[2]
  28. if len(splitname) ==2:
  29. first = splitname[0]
  30. middle = None
  31. last = splitname[1]
  32.  
  33. house=row["house"]
  34. birth=int(row["birth"])
  35.  
  36. id_count += 1
  37.  
  38. print(id_count, first, middle, last, house, birth)
  39.  
  40. # empty file ready to recieve data
  41.  
  42. open("students.db", "w").close()
  43. db=cs50.SQL("sqlite:///students.db")
  44. db.execute("INSERT INTO students (first, middle, last, house, birth) values (?,?,?,?,?)", first, middle, last, house, birth)
  45.  
  46. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement