How to read/write CSV files in Python

how-to-read-write-csv-files-in-python-csv-module

In this tutorial, we’re gonna look at way to use csv module to read, write CSV files in Python program.

CSV files

CSV stands for ‘comma-separated values’. CSV file is a spreadsheet stored as plain-text file.
Each line in a CSV file represents a row, and commas separate cells in the row.

For example:

Title,Category,Date
Python String methods,Python,12/27/2018
How to iterate over a List in Java,Java,12/21/2018
Kotlin List functions,Kotlin,12/19/2018

Instead of commas, cells could be separated with tab characters:

Title	Tags	Date
Python String methods	python, string	12/27/2018
How to iterate over a List in Python	python, list	12/21/2018
Python List functions	python, list	12/19/2018

CSV file is simple than Excel spreadsheet because it doesn’t have:
– types, everything is just a string
– font size or color
– multiple worksheets
– cell widths and heights
– merge cells
– images or charts

Read CSV file in Python

You can follow these steps to read data from a CSV file:
– import csv module
– open CSV file using open() function
– create a Reader object that lets you iterate over rows
– access values in the Reader object by:
+ convert it to a plain Python list using list() function
+ use for-loop (avoid loading the entire file into memory at once)

# cells are separated with commas
>>> import csv
>>> gkzFile = open('grokonez.csv')
>>> csvReader = csv.reader(gkzFile)
>>> gkzTuts = list(csvReader)
>>> gkzTuts
[['Title', 'Category', 'Date'], ['Python String methods', 'Python', '12/27/2018'], ['How to iterate over a List in Java', 'Java', '12/21/2018'], ['Kotlin List functions', 'Kotlin', '12/19/2018']]
>>> gkzTuts[0][0] # data[row][col]
'Title'
>>> gkzTuts[0][1]
'Category'
>>> gkzTuts[0][2]
'Date'
>>> gkzTuts[2][0]
'How to iterate over a List in Java'
>>> gkzTuts[2][1]
'Java'
>>> gkzTuts[2][2]
'12/21/2018'

# cells are separated with 'tab' characters
>>> gkzFile = open('grokonez.csv')
>>> csvReader = csv.reader(gkzFile, delimiter='\t')
>>> gkzTuts = list(csvReader)
>>> gkzTuts
[['Title', 'Tags', 'Date'], ['Python String methods', 'python, string', '12/27/2018'], ['How to iterate over a List in Python', 'python, list', '12/21/2018'], ['Python List functions', 'python, list', '12/19/2018']]
>>> gkzTuts[0][0]
'Title'
>>> gkzTuts[0][1]
'Tags'
>>> gkzTuts[0][2]
'Date'
>>> gkzTuts[2][0]
'How to iterate over a List in Python'
>>> gkzTuts[2][1]
'python, list'
>>> gkzTuts[2][2]
'12/21/2018'

Remember that we can loop over Reader object only once.
To reread the CSV file by for-loop, we must create Reader object again.

>>> import csv
>>> gkzFile = open('grokonez.csv')
>>> csvReader = csv.reader(gkzFile, delimiter='\t')
>>> for row in csvReader:
...     print(str(csvReader.line_num) + ': ' + str(row))
...
1: ['Title', 'Tags', 'Date']
2: ['Python String methods', 'python, string', '12/27/2018']
3: ['How to iterate over a List in Python', 'python, list', '12/21/2018']
4: ['Python List functions', 'python, list', '12/19/2018']

Write CSV file in Python

You can follow these steps to write data to a CSV file:
– import csv module
– create new CSV file using open() function in write mode
– create a Writer object using csv.writer() function
– use Writer object writerow() method to write a line (form of a list). The return value of writerow() is the number of characters for that row (including newline characters).

>>> import csv
>>> gkzFile = open('grokonez.csv', 'w', newline='')
>>> writer = csv.writer(gkzFile)
>>> writer.writerow(['Title', 'Tags', 'Date'])
17
>>> writer.writerow(['Python String methods', 'Python', '12/27/2018'])
41
>>> writer.writerow(['How to iterate over a List in Java', 'Java', '12/21/2018'])
52
>>> writer.writerow(['Kotlin List functions', 'Kotlin', '12/19/2018'])
41
>>> gkzFile.close()

Result in grokonez.csv file:

Title,Tags,Date
Python String methods,Python,12/27/2018
How to iterate over a List in Java,Java,12/21/2018
Kotlin List functions,Kotlin,12/19/2018

*Note: On Windows, newline keyword argument of open() function helps us avoid double-spaced when writing a new row in the output CSV file.

If we want to separate cells and end rows with a specific character, just pass delimiter and lineterminator as keyword arguments of csv.writer() function:

>>> import csv
>>> gkzFile = open('grokonez.csv', 'w', newline='')
>>> writer = csv.writer(gkzFile, delimiter='\t', lineterminator='\n\n')
>>> writer.writerow(['Title', 'Tags', 'Date'])
17
>>> writer.writerow(['Python String methods', 'python, string', '12/27/2018'])
49
>>> writer.writerow(['How to iterate over a List in Python', 'python, list', '12/21/2018'])
62
>>> writer.writerow(['Python List functions', 'python, list', '12/19/2018'])
47
>>> gkzFile.close()

Result in grokonez.csv file:

Title	Tags	Date

Python String methods	python, string	12/27/2018

How to iterate over a List in Python	python, list	12/21/2018

Python List functions	python, list	12/19/2018



By grokonez | February 4, 2019.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*