How to read/write CSV files in Python
Tags: python, csv, readfile, write
Published: 2021-05-16 11:24:09

https://grokonez.com/python/how-to-read-write-csv-files-in-python-csv-module

How to read/write CSV files in Python

In this tutorial, we're gonna look at way to use <code>csv</code> module to read, write CSV files in Python program.

<!--more-->
<h2>CSV files</h2>
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 <em>commas</em> separate cells in the row.

For example:
<pre><code class="language-javascript">
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
</code></pre>
Instead of <em>commas</em>, cells could be separated with <em>tab</em> characters:
<pre><code class="language-javascript">
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
</code></pre>
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
<h2>Read CSV file in Python</h2>
You can follow these steps to read data from a CSV file:
- import <code>csv</code> module
- open CSV file using <code>open()</code> function
- create a <code>Reader</code> object that lets you iterate over rows
- access values in the <code>Reader</code> object by:
+ convert it to a plain Python list using <code>list() </code> function
+ use for-loop (avoid loading the entire file into memory at once)
<pre><code class="language-python">
# 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'
</code></pre>
Remember that we can loop over <code>Reader</code> object <strong><em>only once</em></strong>.
To reread the CSV file by for-loop, we must create <code>Reader</code> object again.

More at:

https://grokonez.com/python/how-to-read-write-csv-files-in-python-csv-module

How to read/write CSV files in Python