0

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?

1
  • As per check50: "Did you mistakenly open your file in append mode?" Commented Jun 5, 2023 at 9:47

1 Answer 1

0

lineterminator of csv module while writing to new csv file, 'lineterminator` defaulted to "\r\n", for test it should be "\r", csv module does it's own line handling

1
  • unclear what you are getting at. this does not appear to be something the user is doing wrong.
    – UpAndAdam
    Commented Feb 21 at 16:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .