Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 14360

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

$
0
0

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 fileCausescourgify.py does not produce CSV with specified formatLogrunning python3 scourgify.py before.csv after.csv...checking that program exited with status 0...checking that after.csv exists...:| scourgify.py cleans long CSV fileCausecan't check until a frown turns upside down

Here is my overall code implementation:

import sysimport csvdef 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_datadef 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.


Viewing all articles
Browse latest Browse all 14360

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>