CSV Reader and Writer

CSV is one of the commonly used data format in data science and as well for many purposes we used comma separated files(csv), tab delimited files etc. So, today will check how to use “CSV module of Python” for reading and writing files.In [20]:
import csv

Reader in csv file

In [21]:
with open('student_details.csv' , 'r') as csv_file1:
     file1 = csv.reader(csv_file1)
## It will print the csv reader object.
     print(file1)
## It will print each line as a list
     for line in file1:
         print(line)
<_csv.reader object at 0x000002D96F31BBA8>
['Name ', 'Roll no.', 'Class ']
['Akshay', '1', '2']
['Rajesh', '24', '3']
['Bram', '12', '6']
['Srishika', '45', '7']
['Radhika', '78', '6']
['Bluenix', '67', '6']

If you wants to remove the first line i.e, header part.

In [22]:
with open('student_details.csv' , 'r') as csv_file1:
     file1 = csv.reader(csv_file1)
     ## It will print the csv reader object.
     print(file1)
     ## It will start pointing to second line as it is working as a generator.                                  next(file1)
     ## It will print each line as a list
     for line in file1:
         print(line)
<_csv.reader object at 0x000002D96F31BC10>
['Akshay', '1', '2']
['Rajesh', '24', '3']
['Bram', '12', '6']
['Srishika', '45', '7']
['Radhika', '78', '6']
['Bluenix', '67', '6']
In [23]:
## delimiter is also a parameter in function reader sometimes, "," is not a separtor  then we need to mention.

with open('student_details.csv' , 'r') as csv_file1:
     file1 = csv.reader(csv_file1, delimiter =",")
     ## It will print the csv reader object.
     print(file1)
     ## It will start pointing to second line as it is working as a generator.    next(file1)
     ## It will print each line as a list
     for line in file1:
         print(line)
<_csv.reader object at 0x000002D96F31BD48>
['Akshay', '1', '2']
['Rajesh', '24', '3']
['Bram', '12', '6']
['Srishika', '45', '7']
['Radhika', '78', '6']
['Bluenix', '67', '6']

Writing in CSV file

Here we will look at how to write data into a file using csv writer. First will read from a csv file and then write the same content in anotheree csv file.In [24]:
with open('student_details.csv' , 'r') as csv_file1:
     ## Reading csv file
     file1 = csv.reader(csv_file1)
     ## Now we are creating new file to write a content.
     ## If this file doesn't exist already then it will create on its own.
     ## Opening in write mode.
     with open('student_details2.csv' , 'w') as csv_file2:
     ## Using a '\t' delimiter to store values.
         file2 = csv.writer(csv_file2, delimiter='-')
     ## Reading line from file1 and then writing in a new created csv file.
         for line in file1:
             file2.writerow(line)

Reading a newly written file.

In [28]:
with open('student_details2.csv' , 'r') as csv_file2:
     ## Reading csv file
     ## Here without delimiter specified
     file2 = csv.reader(csv_file2)
     ## Output is different as it uses \t as raw form.
     for line in file2:
         print(line)
['Name \tRoll no.\tClass ']
['Akshay\t1\t2']
['Rajesh\t24\t3']
['Bram\t12\t6']
['Srishika\t45\t7']
['Radhika\t78\t6']
['Bluenix\t67\t6']

See how with delimiter our output gets changed.

In [29]:
with open('student_details2.csv' , 'r') as csv_file2:
     ## Reading csv file
     ## Here without delimiter specified
     file2 = csv.reader(csv_file2, delimiter="\t")
     ## Output is different as it uses \t as raw form.
     for line in file2:
         print(line)
['Name ', 'Roll no.', 'Class ']
['Akshay', '1', '2']
['Rajesh', '24', '3']
['Bram', '12', '6']
['Srishika', '45', '7']
['Radhika', '78', '6']
['Bluenix', '67', '6']

DictReader()

It will read the file in dictionary format i.e, key and value pairs. There is DictWriter() as well, I would recommend you to explore on your own. Approach documentation as well.In [30]:
with open('student_details2.csv' , 'r') as csv_file2:
     ## Reading csv file
     ## Here without delimiter specified
     file2 = csv.DictReader(csv_file2, delimiter="\t")
     ## Output is different as it uses \t as raw form.
     for line in file2:
         print(line)
{'Class ': '2', 'Roll no.': '1', 'Name ': 'Akshay'}
{'Class ': '3', 'Roll no.': '24', 'Name ': 'Rajesh'}
{'Class ': '6', 'Roll no.': '12', 'Name ': 'Bram'}
{'Class ': '7', 'Roll no.': '45', 'Name ': 'Srishika'}
{'Class ': '6', 'Roll no.': '78', 'Name ': 'Radhika'}
{'Class ': '6', 'Roll no.': '67', 'Name ': 'Bluenix'}