CSV Writer in Python with Example

Python provides the writer class in csv module to write csv records into files - either one row or multiple rows at a time.

Csvwriter Updated 6 Jan 2017 1 min read

CSV Writer in Python

Real time data feeds are so pervasive in all walks of our lives. Every day our smart phones deal with lot of real time data feeds like Weather Feeds, Traffic Updates, Stock Quotes and so on. CSV is the most common format used for dissemination as well as storage of these real time data. In Python the csv.Writer class of csv module provides an implementation of CSV writing logic and makes developers life lot easier.

Python CSV Writer Example

python
import csv

with open('./quotes.csv', 'w', newline='') as QuotesFile:

    quote1 = ["MSFT", "62.30", "62.48", "63.41", "62.12", "487.83", "29.79", "2.50"]

    CSVWriter = csv.writer(QuotesFile, delimiter=',',quotechar='"')

    CSVWriter.writerow(quote1)

    # Write Next quote

    # The quote for MSFT changes every fraction of a second

    quote2 = ["MSFT", "62.28", "62.48", "63.41", "62.12", "487.83", "29.79", "2.50"]

    CSVWriter.writerow(quote2)

Python CSV Writer Output

Output
MSFT,62.30,62.48,63.41,62.12,487.83,29.79,2.50

MSFT,62.28,62.48,63.41,62.12,487.83,29.79,2.50