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?
How do I write a list of elements in a csv file in Python?
Write List to CSV in Python Using the csv. csv file will be created, row-wise, in the current directory with the entries that are present in the code. The csv. writer() function will create the writer() object, and the writer. writerow() command will insert the entries row-wise into the CSV file.
How do I make a list into a csv file?
The most common method to write data from a list to CSV file is the writerow() method of writer and DictWriter class. Example 1: Creating a CSV file and writing data row-wise into it using writer class.
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
-
r: Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.
-
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.
-
r+: Opens a file for both reading and writing. The file pointer will be at the beginning of the file.
-
rb+: Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.