How to write to CSV and not overwrite past text

前端 未结 3 971
不思量自难忘°
不思量自难忘° 2020-12-06 12:31

The code below is what I have so far. When it writes to the .csv it overwrites what I had previously written in the file.How can I write to the file in such a way that it do

3条回答
  •  Happy的楠姐
    2020-12-06 12:44

    You'll want to open the file in append-mode ('a'), rathen than write-mode ('w'); the Python documentation explains the different modes available.

    Also, you might want to consider using the with keyword:

    It is good practice to use the with keyword when dealing with file objects. This has the advantage that the file is properly closed after its suite finishes, even if an exception is raised on the way.

    >>> with open('/tmp/workfile', 'a') as f:
    ...     f.write(your_input)
    

提交回复
热议问题