Read-write CSV files in Python

Python PythonFile

In this post, we are going to understand how to Read-write CSV files in Python. Python CSV library is used to read and write CSV files. In this post, we are going to learn Python CSV Library with an example.CSV files containing the data in tabular form, and the CSV library can help load this data from the CSV files.

1. Read file using reader() function


The CSV file is open using a built-in open() function that returns a file object and which passes it to the reader.

  • First need to import the CSV library using code import csv.
  • Open file using ‘with’ statement no need to close the file manually. The CSV.reader() return a iterable reader object.
  • The reader() function split the row by specified delimiter(comma default) and return a list of string.

Sample Data

1,201,New Delhi,Sachin
2,202,Noida,Dravid
3,203,Gurugram,Sourav
4,204,Faridabad,Kohli
5,205,,Dhoni

Python Program to Read CSV using CSV.reader()

In our example, above we are not passing any arguments to it will read the file and return data as a list of strings.

import csv

with open('PlayersData.csv') as datafile:

    data_reader = csv.reader(datafile)

    for row in data_reader:
        print(row)

Output

 ['1', '201', 'New Delhi', 'Sachin']
['2', '202', 'Noida', 'Dravid']
 ['3', '203', 'Gurugram', 'Sourav']
['4', '204', 'Faridabad', 'Kohli']
 ['5', '205', '', 'Dhoni']

2. Read CSV by passing second argument as quoting to reader()


The reader() argument quoting=csv.QUOTE_ALL is used to specify reader object that all values in CSV file are present inside quotation marks. we are using a for loop to iterate on each row at a time.

1,201,New Delhi,Sachin
2,202,Noida,Dravid
3,203,Gurugram,Sourav
4,204,Faridabad,Kohli
5,205,,Dhoni

Program Example

import csv
with open('PlayersData.csv') as csv_file:
    csv_reader = csv.reader(csv_file,quoting=csv.QUOTE_ALL,skipinitialspace=True)
    for row in csv_reader:
        print("Line Number   ",csv_reader.line_num,"  ",row )

Output

Line Number    1    ['1', '201', 'New Delhi', 'Sachin']
Line Number    2    ['2', '202', 'Noida', 'Dravid']
Line Number    3    ['3', '203', 'Gurugram', 'Sourav']
Line Number    4    ['4', '204', 'Faridabad', 'Kohli']
Line Number    5    ['5', '205', '', 'Dhoni']

3. DictReader() to Read CSV file into Dictionary


The CSV library CSV.DictReader() use to read a CSV file directly into dictionary.The first rows of the CSV file are contained columns, so all the columns are used as keys of the dictionary.

Program Example

import csv
with open('PlayersData.csv') as datafile:
    data_reader = csv.DictReader(datafile)
    for row in data_reader:
        print(row)

Output

{'1': '2', '201': '202', 'New Delhi': 'Noida', 'Sachin': 'Dravid'}
{'1': '3', '201': '203', 'New Delhi': 'Gurugram', 'Sachin': 'Sourav'}
{'1': '4', '201': '204', 'New Delhi': 'Faridabad', 'Sachin': 'Kohli'}
{'1': '5', '201': '205', 'New Delhi': '', 'Sachin': 'Dhoni'}

if the file does not have columns We can specify columns that will use as keys and read that values from files to match the columns

columns = ['S.No', 'Badge No', 'City', 'PlayerName']
with open('PlayersData.csv') as csv_file:
    data_reader = csv.DictReader(csv_file, fieldnames=columns)
    for row in data_reader:
        print(row['PlayerName'] + "   " + row['Badge No'])

Output

Sachin   201
Dravid   202
Sourav   203
Kohli   204
Dhoni   205

2. Python CSV dictwriter() method


The dictwriter() method is used to write a dictionary to CSV. It maps the dictionaries into CSV file rows.

Syntax

csv.DictWriter(f, fieldnames, restval='', extrasaction='raise', dialect='excel', *args, **kwds)

Parameters

  • Fieldnames :This represents the sequence of key that recognise the order in which values in dictionary are passed to the writerow() method or identify the column name write to CSV file.
  • restval: The value to be written if the dictionary is missing a key in fieldnames.
  • extrasaction: This parameter indicates what action to take. If extrasaction is set to ‘raise’, the default value, a ValueError is raised. If it is set to ‘ignore’, extra values in the dictionary are ignored.

2.1 Write list of dictionaries to CSV with Custom headers


First, we need to import the CSV module to use all its methods and convert lists of dictionaries to CSV. The CSV module’s dictwriter() method takes the name of the CSV file and a list of columns’ names as an argument. This can be used for a directory or multiple dictionaries, nested dictionaries.

The writeheader() method writes the first rows of the column name in the CSV file that is the header row.

Now we need to write data to CSV. The writerow() method is used to write the single row at a time and map the dictionary into CSV rows.

Python code to list of dictionaries to CSV with headers

import csv
columns = ['Name','Mark','Subject']
Student_dict = [
{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
{ 'Name': 'Rack', 'Mark': 100,'Subject': 'Math'},
{ 'Name': 'Max', 'Mark': 100,'Subject': 'Music'},
{ 'Name': 'David', 'Mark':100,'Subject': 'Math'},
{ 'Name': 'Tawn', 'Mark': 100,'Subject': 'Physic'},
]
 
try:
    with open("students.csv", 'w',newline='') as csvfile:
        writer = csv.DictWriter(csvfile, fieldnames=columns)
        writer.writeheader()
        for key in Student_dict:
            writer.writerow(key)
except IOError:
    print("I/O error")

Output

Name,Mark,Subject
Jack,100,Math
Rack,100,Math
Max,100,Music
David,100,Math
Tawn,100,Physic