Python code to write .csv file and read data from the .csv file
Write the Python code to write the .csv file and read the data form .csv file
Here I am going to show you how to write the .csv file
To write .csv file or read data from .csv file in python we need to use csv module. Csv module as a method to read and write the comma separated values
Code to write .csv file:-
import csv
if __name__=='__main__':
file='employeinfo.csv'
mode='w'
with open(file=file,mode=mode) as myfile:
csv_write=csv.writer(myfile)
csv_write.writerow(["ENO","ENAME","EAGE"])
record_count=int(input("Enter the number of records to be inserted:"))
for count in range(record_count):
eno=int(input("Enter the employee number"))
ename=input("Enter the employee Name")
eage=int(input("Enter the employee age"))
csv_write.writerow([eno,ename,eage])
print("Employee information has been added to the csv file")
snapshot of code:-
Here I am going to show you how to read data from .csv file and display that on the console.
Code to read .csv file:-
import csv
if __name__=='__main__':
file='employeinfo.csv'
mode='r'
with open(file=file,mode=mode) as myfile:
csv_reader=csv.reader(myfile)
employee_record=list(csv_reader)
print("The Employee Records are")
for line in employee_record:
for word in line:
print(word,'\t',end=' ')
print()
Snapshot of Code:-
Snapshot of Output:-
Related Videos:-
Leave a Comment