Search code examples
pythoncsvfileread-csv

:( scourgify.py cleans short CSV file || :| scourgify.py cleans long CSV file


I am trying to solve CS50's scourgify problem. In the file called before.csv there are two columns name, house respectively. The task is to split name column into two independent columns namely first and last. After then create new file and write this information: first, last, house. I almost past all the test cases, but I do not have a clue why am I getting this error messages even my program gives me expected result.

:( scourgify.py cleans short CSV file
Cause
scourgify.py does not produce CSV with specified format
Log
running python3 scourgify.py before.csv after.csv...
checking that program exited with status 0...
checking that after.csv exists...
:| scourgify.py cleans long CSV file
Cause
can't check until a frown turns upside down

Here is my overall code implementation:

import sys
import csv


def main():
    if len(sys.argv) >= 4:
        print("Too many command-line arguments")
        sys.exit(1)

    if len(sys.argv) <= 2:
        print("Too few command-line arguments")
        sys.exit(1)

    filepath1 = sys.argv[1]
    filepath2 = sys.argv[2]
    if not filepath1.endswith(".csv") or not filepath2.endswith(".csv"):
        print("Not a CSV file")
        sys.exit(1)

    data = read_from_file(filepath1)
    writer = write_to_file(filepath2, data)

def read_from_file(filepath):
    updated_data = []
    try:
        with open(filepath, "r") as file:
            data = csv.DictReader(file)

            for i in data:
                name = i["name"]
                house = i["house"]
                first, last = name.split(", ")
                new_dict = {
                    "first": first,
                    "last": last,
                    "house": house,
                }
                updated_data.append(new_dict)

    except FileNotFoundError:
        print("File does not exist")
        sys.exit(1)

    return updated_data


def write_to_file(filepath, new_data):
    fieldnames = ["first", "last", "house"]
    try:
        with open(filepath, mode="w") as file:
            writer = csv.DictWriter(file, fieldnames=fieldnames)
            writer.writeheader()
            writer.writerows(new_data)

    except FileNotFoundError:
        print("File does not exist")
        sys.exit(1)


if __name__ == "__main__":
    main()

I checked some questions relating to this problem set, but they were not helpful. If anybody knows what is the problem, I would be grateful to hear from you.


Solution

  • I found my bug:

                for i in data:
                name = i["name"]
                house = i["house"]
                last, first = name.split(", ")
                new_dict = {
                    "first": first,
                    "last": last,
                    "house": house,
                }
                updated_data.append(new_dict)
    

    When I am looping through each row I made me mistake which is instead of assigning last, first = name.split(", "), I did first, last = name.split(", "). Be careful like this simple errors because it took me sometime to find it.