1

My program fails last two tests with check50 eventhough the output is a csv file with the correct specified columns(first, last, house). Can anyone tell me where the problem is?

Here is my code:

import csv
import sys

def main():
    leng = len(sys.argv)
    if leng < 3:
        sys.exit("Too few command-line arguments")
    elif leng == 3:
       scourgy(sys.argv[1], sys.argv[2])
    elif leng > 3:
        sys.exit("Too many command-line arguments")


def scourgy(file1, file2):
    if is_csv(file1) and is_csv(file2):
        with open(file1, "r", newline="") as before:
            reader = csv.DictReader(before)
            with open(file2, "w", newline="") as after:
                columns = ["first", "last", "house"]
                writer = csv.DictWriter(after, fieldnames=columns)
                writer.writeheader()

            for row in reader:
                splitted = row["name"].split(",")
                first_name = splitted[1]
                last_name = splitted[0]
                house = row["house"]

                with open(file2, "a", newline="") as after:
                    writer = csv.DictWriter(after, fieldnames=columns )
                    writer.writerow({"first": first_name, "last": last_name, "house": house})
    else:
        sys.exit(f"Could not read {file1}")



def is_csv(file):
    splitted = file.split('.')

    try:
        if splitted[1] == "csv":
            return True
        else:
            sys.exit("Not a CSV file")
    except IndexError:
        sys.exit("Not a CSV file")


if __name__ == "__main__":
    main()

Here are the first few line of the ouputted file:

first,last,house
Hannah,Abbott,Hufflepuff
Katie,Bell,Gryffindor
Susan,Bones,Hufflepuff
Terry,Boot,Ravenclaw
Lavender,Brown,Gryffindor
Millicent,Bulstrode,Slytherin
Cho,Chang,Ravenclaw

Here is a link for check50 results: https://submit.cs50.io/check50/e0618d0090b80b2ca35ae787655865d2e186f757

Thanks for your help.

1 Answer 1

7

What comes after the comma in the "before" name? Therefore, what is the first character in the "after" first name? The after file really looks like this:

first,last,house
 Hannah,Abbott,Hufflepuff
 Katie,Bell,Gryffindor
 Susan,Bones,Hufflepuff
1
  • Thanks, i had a doubt it was that space at the beginning but i wasn't sure!
    – khalil_dev
    Commented Aug 17, 2022 at 16:16

You must log in to answer this question.

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