Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
edited tags
Link
UpAndAdam
  • 1.7k
  • 1
  • 3
  • 20
Source Link

CS50P PSet 6 check50 problem for scourgify

I managed to write the code to make the csv specific, yet the last two checks for check50 are still coming as errors. Here is my code:

import csv
import sys

students = []

if len(sys.argv) < 3:
    sys.exit("Too few command-line arguments")
elif len(sys.argv) > 3:
    sys.exit("Too many command-line arguments")

try:
    with open(sys.argv[1]) as file:
        reader = csv.DictReader(file)
        for row in reader:
            students.append(row)
except FileNotFoundError:
    sys.exit(f"Could not read {sys.argv[1]}")

for student in students:
    name = student["name"].split(",")
    first = name[1]
    last = name[0]

    student["name"] = first.strip()
    student["last"] = last

with open(sys.argv[2], "a") as file:
    writer = csv.DictWriter(file, fieldnames=["first", "last", "house"])
    writer.writeheader()
    for student in students:
        writer.writerow({"first": student["name"], "last": student["last"], "house": student["house"]})

Is there a problem that I am possibly overlooking?