You are currently viewing How to read and write data in CSV  File in Python

Before starting the operation on CSV files let us see what is a CSV file and how can you create a CSV file. A CSV  is a comma-separated value,  A CSV file stores tabular data in plain text. This is a widely used data format to import and export data. You can use tools like Notepad, NotePad++, Libre Office, Open Office or Microsoft Excel to create or read a CSV file. In the examples in this article, we will be using Microsoft Excel.

How to create the CSV file?

  • Open the Microsoft Excel and create the new file and save the file with extension .csv you can simply create the text file and save the text file with .csv extension. Here I am creating the file with name jammu and saving it with extension .csv

How to write the data in CSV file in Python

To write the data in the CSV file we are using the Python File handling. I have created the CSV file with the name jammu. We will write some basic details like Name, Age and Salary of employees. We will take input from the user and then write those values in the CSV file. Below is the code for Python File handling to write the data in the CSV file.

import csv  # importing the csv library

file=open('D:\jammu.csv','w')  # opening the file in write Mode
head=['Name','Age','Salary'] # setting the header in csv file
writer=csv.DictWriter(file,fieldnames=head)
writer.writeheader()
for x in range(2):
  name=input('Enter Name ')
  age=input('Enter age ' )
  salary=input('Enter Salary ')
  writer.writerow({'Name':name,'Age':age,'Salary':salary})
  
file.close()  

To write the value to the file, first, you have to import the CSV library. In the above code, we are taking two inputs from a user and writing the data in the CSV file. Below you can see how data is getting saved in CSV file

How to read the CSV file in Python

Now let us read the CSV file, Let use the same CSV file to read the data. To read the data from the CSV file, you can use below is the code.

import csv  #import Csv Library

file=open('D:\jammu.csv')
reader=csv.DictReader(file)
for row in reader:
  print(row['Name'] ,row['Age'],row['Salary'])
  
file.close()  

Python Program to search the string in CSV File

Suppose we have large data, that we are reading and we have to search for some terms within that CSV file. This can also be achieved using the program below. This program will search the name stored in the CSV file. If the name is found in the CSV then it will give all detail related to the name. I am using the above CSV file in which we had stored some data.

import csv  
name=input("enter tha name")
file=open('D:\jammu.csv')
reader=csv.DictReader(file)
for row in reader:
  if row['Name']==name:
    print(row['Name'] ,row['Age'],row['Salary'])
  
file.close()  

That’s it Folks on this topic. Let us know your comments on this article in the comment box below. Kindly like our facebook page, follows us on twitter and subscribe to our YouTube channel for latest updates.

Leave a Reply