Monday, September 21, 2020

Python File Handling - CSV Files

Introduction

CSV (Comma Separated Values) is a simple file format used to store tabular data, such as a spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text. Each line of the file is a data record. Each record consists of one or more fields, separated by commas.

Opening a CSV file: CSV file could be open using the open function

In [1]:
import csv
dt=open("data2.csv")
for i in dt:
    print(i)
print(dt)
,c1,c2

r1,0,1

r2,2,3

r3,4,5

<_io.TextIOWrapper name='data2.csv' mode='r' encoding='cp1252'>

Reading from a CSV file:

In [2]:
# USING csv.reader() function

csv.reader() arguments: reader(iterable [, dialect='excel'] , [optional keyword args])

description:

  • iterable: CSV file name is passed
  • dialect: format of the file, default is excel
  • optional arguments: here we can pass delimiter argument which by default is ',' but can be '\t', etc also

In [3]:
import csv
with open('profData.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
['Name', 'Age', 'Profession']
['Jack', '23', 'Doctor']
['Miller', '22', 'Engineer']
['john', ' 34', ' actor']

NOTE: by using the with keyword, we dont need to close the file

As we can see that the rows of the csv file is returned as a list, now we can access these list and perform operation on them

reading a file with tab as delimiter(i.e. data separator)

In [4]:
import csv
with open('profData.csv', 'r',) as file:
    reader = csv.reader(file, delimiter = '\t') # note we have passed a \t as delimiter
    for there in reader:
        print(row)
['john', ' 34', ' actor']
['john', ' 34', ' actor']
['john', ' 34', ' actor']
['john', ' 34', ' actor']

Writing to a CSV file:

In [11]:
# using csv.writer() function

Function details : csv.writer()

writer(fileobj [, dialect='excel'] ,[optional keyword args])

  • fileobj: the file handle
  • dialect: format of the file, default is excel
  • optional arguments: here we can pass delimiter argument which by default is ',' but can be '\t', etc also
In [15]:
import csv
with open('protagonist.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["SN", "Movie", "Protagonist"])
    writer.writerow([1, "Lord of the Rings", "Frodo Baggins"])
    writer.writerow([2, "Harry Potter", "Harry Potter"])
    
with open('protagonist.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
['SN', 'Movie', 'Protagonist']
['1', 'Lord of the Rings', 'Frodo Baggins']
['2', 'Harry Potter', 'Harry Potter']
In [16]:
# using the csv.writerows() function
In [17]:
import csv
csv_rowlist = [["SN", "Movie", "Protagonist"], [1, "pirates of the caribbean", " Captain Jack Sparrow"],
               [2, "john wick", "john wick"]]
with open('protagonist2.csv', 'w') as file:
    writer = csv.writer(file)
    writer.writerows(csv_rowlist)
    
with open('protagonist2.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)
['SN', 'Movie', 'Protagonist']
[]
['1', 'pirates of the caribbean', ' Captain Jack Sparrow']
[]
['2', 'john wick', 'john wick']
[]
Share: