使用python时出现AttributeError

2024-06-16 13:26:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图让下面的脚本工作,但它不工作。。。 有人能帮我吗?你知道吗

if happy == "no":
        import csv
        csvbestand='improvements.csv'
        try:
            f = open('improvements.csv', 'w', newline='')
            writer = csv.writer(f, delimiter=',')
            data = [(input("What is your name?: ")),
                    (input("What do you want to improve? "))]
            f.writerows(data)
        finally:
            f.close()

我确实得到以下错误:

Traceback (most recent call last):
  File "C:/Users/maxva/OneDrive/Documents/HU/HU/Programming/Huiswerk/Week 4/Weekopdracht/test.py", line 31, in <module>
    f.writerows(data)
AttributeError: '_io.TextIOWrapper' object has no attribute 'writerows'

Process finished with exit code 1

此外,是否有可能在每次运行程序时保护输入? 现在,当我运行这个程序时,它是CSV文件中的输入,但是下次我运行它时,他会删除以前的数据,但是我想保留以前的数据。你知道吗

我希望你能帮我:)


Tags: csv数据noimport程序脚本inputdata
1条回答
网友
1楼 · 发布于 2024-06-16 13:26:47

Thanks for the answers! It worked. But how about the fact that the previous data removes when I run the program again?

已经开始工作了!我把这里的“w”改成了“a”:

open(csvbestand1, 'a', newline='')

相关问题 更多 >