I am experiencing errors on the last two check50 test cases. I checked my csv output and it seems correct to me. Anyone knows what I have done wrong in my code?
import sys
import csv
def scourgify(fileName, fileOutput):
with open(fileName, "r") as file:
dict, names, after = [], [], []
reader = csv.DictReader(file, fieldnames = ["name", "house"])
for row in reader:
dict.append(row)
for i in range(len(dict)-1):
name = (dict[i+1]["name"].split(", "))
after.append([name[1], name[0], dict[i+1]["house"]])
with open(fileOutput, "a", newline = "") as file2:
writer = csv.DictWriter(file2, fieldnames = ["first", "last", "house"])
writer.writerow({"first": "first", "last": "last", "house": "house"})
for j in range(len(after)):
writer.writerow({"first": after[j][0], "last": after[j][1], "house": after[j][2]})
return None
def main():
try:
if len(sys.argv) > 3:
sys.exit("Too many command-line arguments")
elif len(sys.argv) < 3:
sys.exit("Too few command-line arguments")
else:
scourgify(sys.argv[1], sys.argv[2])
except FileNotFoundError:
sys.exit("Could not read " + sys.argv[1])
if __name__ == "__main__":
main()
Here is what is being outputted to after.csv:
first,last,house
Hannah,Abbott,Hufflepuff
Katie,Bell,Gryffindor
Susan,Bones,Hufflepuff
Terry,Boot,Ravenclaw
Lavender,Brown,Gryffindor
Millicent,Bulstrode,Slytherin
Cho,Chang,Ravenclaw
Penelope,Clearwater,Ravenclaw
Vincent,Crabbe,Slytherin
Colin,Creevey,Gryffindor
Dennis,Creevey,Gryffindor
Cedric,Diggory,Hufflepuff
Marietta,Edgecombe,Ravenclaw
Justin,Finch-Fletchley,Hufflepuff
Seamus,Finnigan,Gryffindor
Anthony,Goldstein,Ravenclaw
Gregory,Goyle,Slytherin
Hermione,Granger,Gryffindor
Angelina,Johnson,Gryffindor
Lee,Jordan,Gryffindor
Neville,Longbottom,Gryffindor
Luna,Lovegood,Ravenclaw
Remus,Lupin,Gryffindor
Draco,Malfoy,Slytherin
Scorpius,Malfoy,Slytherin
Ernie,Macmillan,Hufflepuff
Minerva,McGonagall,Gryffindor
Eloise,Midgen,Gryffindor
Cormac,McLaggen,Gryffindor
Graham,Montague,Slytherin
Theodore,Nott,Slytherin
Pansy,Parkinson,Slytherin
Padma,Patil,Gryffindor
Parvati,Patil,Gryffindor
Harry,Potter,Gryffindor
Tom,Riddle,Slytherin
Demelza,Robins,Gryffindor
Newt,Scamander,Hufflepuff
Horace,Slughorn,Slytherin
Zacharias,Smith,Hufflepuff
Severus,Snape,Slytherin
Alicia,Spinnet,Gryffindor
Pomona,Sprout,Hufflepuff
Dean,Thomas,Gryffindor
Romilda,Vane,Gryffindor
Myrtle,Warren,Ravenclaw
Fred,Weasley,Gryffindor
George,Weasley,Gryffindor
Ginny,Weasley,Gryffindor
Percy,Weasley,Gryffindor
Ron,Weasley,Gryffindor
Oliver,Wood,Gryffindor
Blaise,Zabini,Slytherin
Here is my check50 result:https://submit.cs50.io/check50/1266cda212440a9c6e4d878b19526536872c2a8d
with open(fileOutput, "a", newline = "") as file2:
?