csv 在通过CS50检查时遇到了问题集6

bvhaajcl  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(112)

当我自己运行程序时,它看起来还可以,但我无法通过check50测试。
这是check50的输出:

:( 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

下面是我的代码:

import sys
import csv

def main():
    check_command_line_arguments()
    try:
        with open(sys.argv[2], "r") as check_file:
            check_reader = csv.reader(check_file)
            for row in check_reader:
                pass
    except FileNotFoundError:
        try:
            with open(sys.argv[2], "a") as new_file:
                header = True
                with open(sys.argv[1]) as old_file:
                    old_file = csv.DictReader(old_file)
                    for row in old_file:
                        name = row["name"].replace(" ", "")
                        house = row["house"]
                        first, last = name.split(",")
                        writer = csv.DictWriter(new_file, fieldnames=["first", "last", "house"])
                        if header == True:
                            writer.writeheader()
                            header = False
                        writer.writerow({"first": first, "last": last, "house": house})

        except FileNotFoundError:
            sys.exit(f"Could not read {sys.argv[1]}")

def check_command_line_arguments():
    if len(sys.argv) < 3:
        sys.exit("To few command-line arguments")
    if len(sys.argv) > 3:
        sys.exit("To many command-line arguments")
    if not sys.argv[2].endswith(".csv"):
        sys.exit("The new file is not a CSV file")

if __name__ == "__main__":
    main()

下面是我的after.csv文件:

first,last,house
Abbott,Hannah,Hufflepuff
Bell,Katie,Gryffindor
Bones,Susan,Hufflepuff
Boot,Terry,Ravenclaw
Brown,Lavender,Gryffindor
Bulstrode,Millicent,Slytherin
Chang,Cho,Ravenclaw
Clearwater,Penelope,Ravenclaw
Crabbe,Vincent,Slytherin
Creevey,Colin,Gryffindor
Creevey,Dennis,Gryffindor
Diggory,Cedric,Hufflepuff
Edgecombe,Marietta,Ravenclaw
Finch-Fletchley,Justin,Hufflepuff
Finnigan,Seamus,Gryffindor
Goldstein,Anthony,Ravenclaw
Goyle,Gregory,Slytherin
Granger,Hermione,Gryffindor
Johnson,Angelina,Gryffindor
Jordan,Lee,Gryffindor
Longbottom,Neville,Gryffindor
Lovegood,Luna,Ravenclaw
Lupin,Remus,Gryffindor
Malfoy,Draco,Slytherin
Malfoy,Scorpius,Slytherin
Macmillan,Ernie,Hufflepuff
McGonagall,Minerva,Gryffindor
Midgen,Eloise,Gryffindor
McLaggen,Cormac,Gryffindor
Montague,Graham,Slytherin
Nott,Theodore,Slytherin
Parkinson,Pansy,Slytherin
Patil,Padma,Gryffindor
Patil,Parvati,Gryffindor
Potter,Harry,Gryffindor
Riddle,Tom,Slytherin
Robins,Demelza,Gryffindor
Scamander,Newt,Hufflepuff
Slughorn,Horace,Slytherin
Smith,Zacharias,Hufflepuff
Snape,Severus,Slytherin
Spinnet,Alicia,Gryffindor
Sprout,Pomona,Hufflepuff
Thomas,Dean,Gryffindor
Vane,Romilda,Gryffindor
Warren,Myrtle,Ravenclaw
Weasley,Fred,Gryffindor
Weasley,George,Gryffindor
Weasley,Ginny,Gryffindor
Weasley,Percy,Gryffindor
Weasley,Ron,Gryffindor
Wood,Oliver,Gryffindor
Zabini,Blaise,Slytherin
ewm0tg9j

ewm0tg9j1#

仔细查看输入和输出文件(. csv之前和之后)。
.csv之前的内容为:

name,house
"Abbott, Hannah",Hufflepuff
"Bell, Katie",Gryffindor

在.csv之后是:

first,last,house
Abbott,Hannah,Hufflepuff
Bell,Katie,Gryffindor

汉娜和凯蒂是名字。

相关问题