英文:
scourgify.py cleans short CSV file || scourgify.py cleans long CSV file
问题
我正在尝试解决CS50的scourgify问题。在名为before.csv的文件中,有两列分别是name和house。任务是将name列拆分为两个独立的列,分别为first和last。然后创建一个新文件,并写入这些信息:first、last、house。我几乎通过了所有的测试用例,但我不知道为什么会收到这些错误消息,即使我的程序给出了预期的结果。
:( scourgify.py清理短CSV文件
原因
scourgify.py未生成指定格式的CSV
日志
运行python3 scourgify.py before.csv after.csv...
检查程序是否以状态0退出...
检查after.csv是否存在...
:| scourgify.py清理长CSV文件
原因
直到一个愁容扭转为笑容才能检查
这是我的整体代码实现:
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 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.
答案1
得分: 2
我找到了我的错误:
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)
当我循环遍历每一行时,我犯了一个错误,即不是赋值为 last, first = name.split(", ")
,而是写成了 first, last = name.split(", ")
。要小心这种简单的错误,因为花了我一些时间才找到它。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论