17

I have the following source code, where I am trying to write a list in a csv file. I need every new list to be written in a new line of this csv file. The source code is the following:

import csv
list1=[55,100,'dir1/dir2/dir3/file.txt',0.8]

resultFile = open("output.csv",'wa')
wr = csv.writer(resultFile, dialect='excel')
wr.writerow(list1)
resultFile.close()

The problem is that it doesn't insert list1 in a newline every time i run the code.

In matlab that would be easy, I just need to use dlmwrite with '-append' parameter.

But how to do this in Python?

mad
  • 2,677
  • 8
  • 35
  • 78

2 Answers2

35

Open file in append mode.

import csv
list1=[58,100,'dir1/dir2/dir3/file.txt',0.8]

with open("output.csv", "a") as fp:
    wr = csv.writer(fp, dialect='excel')
    wr.writerow(list1)

More on file open modes

try following:-

>>> with open('test1','wb') as f: f.write('test')
... 
>>> with open('test1','ab') as f: f.write('koko')
... 
>>> with open('test1','rb') as f: f.read()
... 
'testkoko'
>>> with open('test1','wa') as f: f.write('coco')
... 
>>> with open('test1','rb') as f: f.read()
... 
'coco'
>>> 

From this link

Modes: Description

  1. r: Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
  2. rb: Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.
  3. r+: Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
  4. rb+: Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.
  5. w: Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
  6. wb: Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.
  7. w+: Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
  8. wb+: Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.
  9. a: Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
  10. ab: Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.
  11. a+: Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
  12. ab+: Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.
Vivek Sable
  • 9,938
  • 3
  • 40
  • 56
  • It works, thank you. I really guess that I did this before and didn't work. Thanks anyway. – mad Feb 02 '15 at 12:07
  • Also the file must be opened with the `b` flag on platforms where that makes a difference, as mentioned in the documentation for [`csv.writer`](https://docs.python.org/2/library/csv.html#csv.writer). – Cristian Ciupitu Feb 28 '15 at 00:58
  • @CristianCiupitu I tried your code, however it writes every new row not in the row below it, but in two below. So that at the end I always have one empty row between two rows with data. How can I fix it? – Euler_Salter Aug 10 '17 at 16:02
  • @Euler_Salter: Can you send me code or some part of code? – Vivek Sable Aug 11 '17 at 05:04
  • @VivekSable well I simply did the following, copying yours: `import csv mylist = [1,2,3,4] with open('somefile.csv', "a") as fp: wr = csv.writer(fp, dialect = 'excel') wr.writerow(mylist)` – Euler_Salter Aug 11 '17 at 11:40
  • its working for me. can u pass me code and output file. vivekbsable@gmail.com – Vivek Sable Aug 11 '17 at 13:56
  • 1
    Thank you for the detailed explanation – KK2491 Mar 18 '21 at 15:05
2

If you use Python 3.x then change your code:

import csv

list1 = [58,100,'dir1/dir2/dir3/file.txt',0.8]

with open("output.csv", "a", newline='') as fp:
    wr = csv.writer(fp, dialect='excel')
    wr.writerow(list1)

Adding newline='' can help you to avoid getting extra new lines, empty rows in between two rows with data.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Sergey Solod
  • 695
  • 7
  • 15