Advertisement
joseleeph

Untitled

Mar 31st, 2021
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #import cs50
  2. #import csv, argv
  3.  
  4. #online edit
  5. #import cs50
  6. #added
  7. #from cs50 import SQL
  8. import cs50
  9. import csv
  10. from sys import argv
  11.  
  12. # must remember to check command line arguements with argv
  13.  
  14. #open("students.db","w").close()
  15.  
  16. studb = cs50.SQL("sqlite:///students.db")
  17. #studb = SQL("sqlite:///students.db") # you don't need cs50 cause you've imported it?
  18. with open("characters.csv", "r") as students:
  19. #with open(argv[1], "r") as students: # name 'argv' is not defined
  20. #reader = csv.DictReader(students, delimiter = ",")
  21. reader = csv.DictReader(students, delimiter = ",")
  22. #forsplit = ""
  23. nmesplt = ""
  24. frstnme = ""
  25. mdlnme = ""
  26. lastnme = ""
  27. #id = 0
  28. for row in reader:
  29. index = 0
  30. nmesplt = row['name'].split(" ")
  31.  
  32. if len(nmesplt) == 3:
  33. frstnme = nmesplt[0]
  34. mdlenme = nmesplt[1]
  35. lastnme = nmesplt[2]
  36.  
  37. #id += 1
  38.  
  39. #studb.execute("INSERT INTO students(id, first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?, ?)", id, frstnme, mdlenme, lastnme, row['house'], row['birth'])
  40. studb.execute("INSERT INTO students(first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)", frstnme, mdlenme, lastnme, row['house'], row['birth'])
  41. if len(nmesplt) == 2:
  42. frstnme = nmesplt[0]
  43. mdlenme = ""
  44. lastnme = nmesplt[1]
  45.  
  46. #id += 1
  47. #studb.execute("INSERT INTO students(id, first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?, ?)", id, frstnme, mdlenme, lastnme, row['house'], row['birth'])
  48. studb.execute("INSERT INTO students(first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?)", frstnme, mdlenme, lastnme, row['house'], int(row['birth']))
  49.  
  50. index += 1
  51.  
  52. #studb.execute("INSERT INTO students(id, first, middle, last, house, birth) VALUES(?, ?, ?, ?, ?, ?)", id, frstnme, mdlenme, lastnme, row['house'], row['birth'])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement