SlideShare a Scribd company logo
KVS RO AGRA
BINARY FILES
&
CSV(COMMA SEPARATED VALUES) FILES
KVS RO AGRA
BINARY FILES
KVS RO AGRA
CREATING BINARY FILES
KVS RO AGRA
Content of binary file which is in codes.
SEEING CONTENT OF BINARY FILE
KVS RO AGRA
READING BINARY FILES TROUGH PROGRAM
CONTENT OF BINARY
FILE
KVS RO AGRA
PICKELING AND UNPICKLING USING PICKLE MODULE
KVS RO AGRA
PICKELING AND UNPICKLING USING PICKEL
MODULE
Use the python module pickle for structured
data such as list or directory to a file.
PICKLING refers to the process of converting
the structure to a byte stream before writing to a
file.
while reading the contents of the file, a
reverse process called UNPICKLING is used to
convert the byte stream back to the original
structure.
KVS RO AGRA
KVS RO AGRA
PICKLING AND UNPICKLING USING PICKEL
MODULE
Firstly we need to import the pickle module, It
provides two main methods:
1) dump() method
2) load() method
KVS RO AGRA
pickle.dump() Method
KVS RO AGRA
pickle.dump() Method
pickle.dump() method write the object in binary file.
Syntax of dump method is:
dump(object ,fileobject)
KVS RO AGRA
pickle.dump() Method
# A program to write list sequence in a binary file
KVS RO AGRA
pickle.load() Method
KVS RO AGRA
pickle.load() Method
pickle.load() method is used to read the binary file.
CONTENT OF BINARY
FILE
KVS RO AGRA
BINARY FILE R/W OPERATION USING PICKLE MODULE
import pickle
Wr_file = open(r"C:UserslenovoDesktoppython filesbin1.bin", "wb")
myint = 56
mylist = ["Python", "Java", "Oracle"]
mystring = "Binary File Operations"
mydict = { "ename": "John", "Desing": "Manager" }
pickle.dump(myint, Wr_file)
pickle.dump(mylist, Wr_file)
pickle.dump(mystring, Wr_file)
pickle.dump(mydict, Wr_file)
Wr_file.close()
R_file = open(r"C:UserslenovoDesktopbin1.bin", "rb")
i = pickle.load(R_file)
s = pickle.load(R_file)
l = pickle.load(R_file)
d = pickle.load(R_file)
print("myint = ", I)
print("mystring =", s)
print("mylist = ", l)
print("mydict = ", d)
R_file.close()
KVS RO AGRA
READING BINARY FILE THROUGH LOOP
Read objects one by one
through loop
import pickle
Wr_file = open(r"C:UserslenovoDesktoppython filesbin1.bin", "wb")
myint = 56
mylist = ["Python", "Java", "Oracle"]
mystring = "Binary File Operations"
mydict = { "ename": "John", "Desing": "Manager" }
pickle.dump(myint, Wr_file)
pickle.dump(mylist, Wr_file)
pickle.dump(mystring, Wr_file)
pickle.dump(mydict, Wr_file)
Wr_file.close()
with open(r"C:UserslenovoDesktopbin1.bin", "rb") as f:
while True:
try:
r=pickle.load(f)
print(r)
print("Next item")
except EOFError:
break
f.close()
KVS RO AGRA
INSERT/APPEND RECORD IN A BINARY FILE
Here we are creating
dictionary Object to
dump it in a binary file
import pickle
Empno = int(input('Enter Employee number:'))
Ename = input('Enter Employee Name:')
Sal = int(input('Enter Salary'))
#Creating the dictionary
dict1 = {'Empno':Empno,'Name':Ename,'Salary':Sal}
#Writing the Dictionary
f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'ab')
pickle.dump(dict1,f)
f.close()
f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'rb')
while True:
try:
dict1 = pickle.load(f)
print('Employee Num:',dict1['Empno'])
print('Employee Name:',dict1['Name'])
print('Employee Salary:',dict1['Salary'])
except EOFError:
break
f.close()
KVS RO AGRA
SEARCH RECORD IN A BINARY FILE
import pickle
f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'rb')
Found = False
eno=int(input("Enter Employee no to be searched"))
while True:
try:
dict1 = pickle.load(f)
if dict1['Empno'] == eno:
print('Employee Num:',dict1['Empno'])
print('Employee Name:',dict1['Name'])
print('Salary',dict1['Salary'])
Found = True
except EOFError:
break
if Found == False:
print('No Records found')
f.close()
KVS RO AGRA
UPDATE RECORD OF A BINARY FILE
import pickle
f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'rb')
rec_File = []
r=int(input("enter Employee no to be updated"))
m=int(input("enter new value for Salary"))
while True:
try:
onerec = pickle.load(f)
rec_File.append(onerec)
except EOFError:
break
f.close()
no_of_recs=len(rec_File)
for i in range (no_of_recs):
if rec_File[i]['Empno']==r:
rec_File[i]['Salary'] = m
f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'wb')
for i in rec_File:
pickle.dump(i,f)
f.close()
KVS RO AGRAimport pickle
f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'rb')
rec_File = []
e_req=int(input("enter Employee no to be deleted"))
while True:
try:
onerec = pickle.load(f)
rec_File.append(onerec)
except EOFError:
break
f.close()
f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'wb')
for i in rec_File:
if i['Empno']==e_req:
continue
pickle.dump(i,f)
f.close()
DELETE RECORD OF A BINARY FILE
KVS RO AGRA
COMMA SEPARATED VALUE(CSV
Files)
KVS RO AGRA
CSV FILE
• CSV is a simple file format used to store tabular data, such as
• a spreadsheet or database.
• Files in the CSV format can be imported to and exported from
programs that store data in tables, such as Microsoft Excel or
OpenOffice Calc.
• CSV stands for "comma-separated values“.
• A comma-separated values file is a delimited text file that uses a
comma to separate values.
• Each line of the file is a data record. Each record consists of
one or more fields, separated by commas. The use of the
comma as a field separator is the source of the name for this file
format
KVS RO AGRA
• One line for each record
• Comma separated fields
• Space-characters adjacent to commas are ignored
• When data has a strict tabular structure
• To transfer large database between programs
• To import and export data to office applications, Qedoc modules
CSV File Characteristics
WHEN USE CSV?
KVS RO AGRA
• CSV is faster to handle
• CSV is smaller in size
• CSV is easy to generate
• CSV is human readable and easy to edit manually
• CSV is simple to implement and parse
• CSV is processed by almost all existing applications
• No standard way to represent binary data
• There is no distinction between text and numeric values
• Poor support of special characters and control characters
• CSV allows to move most basic data only. Complex configurations cannot be imported and
exported this way
• Problems with importing CSV into SQL (no distinction between NULL and quotes)
CSV Advantages
CSV Disadvantages
KVS RO AGRA
CSV file handling in Python
To perform read and write operation with
CSV file,
• we must importcsv module.
• open() function is used toopen file, and
return file object.
KVS RO AGRA
WRITING DATA IN CSV FILE
 import csv module
 Use open() to open CSV file by specifying
mode
“w” or “a”, it will return file object.
 “w” will overwrite previous content
 “a” will add content to the end of previous
content.
 Pass the file object to writer object with
delimiter.
 Then use writerow() to send data in CSV file
KVS RO AGRA
import csv
with open(r'C:UserslenovoDesktoppython filesnew.csv','w') as wr:
a=csv.writer(wr,delimiter=",")
a.writerow(["Roll no","Name","Marks"])
a.writerow(["1","Rahul","85"])
a.writerow(["2","Priya","80"])
wr.close()
Writing to CSV file
KVS RO AGRA
Content of CSV file
KVS RO AGRA
Reading from CSV file
• import csv module
• Use open() to open csv file, it will return file
object.
• Pass this file object to reader object.
• Perform operation you want
KVS RO AGRA
import csv
with open(r'C:UserslenovoDesktoppython filesnew.csv',‘r') as rr:
a=csv.reader(rr)
for i in a:
print(i)
wr.close()
Reading from CSV file
KVS RO AGRA
THANK YOU &
HAVE A NICE DAY
UNDER THE GUIDANCE OF KVS RO AGRA
VEDIO LESSON PREPARED BY:
KIRTI GUPTA
PGT(CS)
KV NTPC DADRI

More Related Content

What's hot (20)

PPTX
Data Analysis with Python Pandas
Neeru Mittal
 
PPTX
Templates in C++
Tech_MX
 
PPTX
DATABASE CONSTRAINTS
sunanditaAnand
 
PPTX
Python ppt
Anush verma
 
PPTX
Unit 4 python -list methods
narmadhakin
 
PPTX
Python: Modules and Packages
Damian T. Gordon
 
PPTX
Intro to Python Programming Language
Dipankar Achinta
 
PPTX
Stacks in c++
Vineeta Garg
 
PDF
Java I/o streams
Hamid Ghorbani
 
PPSX
Modules and packages in python
TMARAGATHAM
 
PDF
ch 2. Python module
Prof .Pragati Khade
 
PPTX
Queue ppt
SouravKumar328
 
PPTX
Python Scipy Numpy
Girish Khanzode
 
PPTX
Python Collections
sachingarg0
 
PPS
Introduction to class in java
kamal kotecha
 
PDF
Numpy - Array.pdf
AnkitaArjunDevkate
 
PDF
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
PDF
STL in C++
Surya Prakash Sahu
 
PPTX
Loops in Python
AbhayDhupar
 
PPTX
Regular expressions in Python
Sujith Kumar
 
Data Analysis with Python Pandas
Neeru Mittal
 
Templates in C++
Tech_MX
 
DATABASE CONSTRAINTS
sunanditaAnand
 
Python ppt
Anush verma
 
Unit 4 python -list methods
narmadhakin
 
Python: Modules and Packages
Damian T. Gordon
 
Intro to Python Programming Language
Dipankar Achinta
 
Stacks in c++
Vineeta Garg
 
Java I/o streams
Hamid Ghorbani
 
Modules and packages in python
TMARAGATHAM
 
ch 2. Python module
Prof .Pragati Khade
 
Queue ppt
SouravKumar328
 
Python Scipy Numpy
Girish Khanzode
 
Python Collections
sachingarg0
 
Introduction to class in java
kamal kotecha
 
Numpy - Array.pdf
AnkitaArjunDevkate
 
Python : Regular expressions
Emertxe Information Technologies Pvt Ltd
 
STL in C++
Surya Prakash Sahu
 
Loops in Python
AbhayDhupar
 
Regular expressions in Python
Sujith Kumar
 

Similar to Data file handling in python binary & csv files (20)

PPTX
How to process csv files
Tukaram Bhagat
 
PPTX
Binary File.pptx
MasterDarsh
 
PPTX
01 file handling for class use class pptx
PreeTVithule1
 
PPTX
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
PPTX
H file handling
missstevenson01
 
PPTX
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
anushasabhapathy76
 
PDF
CSV Files-1.pdf
AmitenduBikashDhusiy
 
PPTX
FILE HANDLING IN PYTHON Presentation Computer Science
HargunKaurGrover
 
PDF
File handling with python class 12th .pdf
lionsconvent1234
 
PPTX
file handling.pptx avlothaan pa thambi popa
senniyappanharish
 
PPTX
File Handling in Python -binary files.pptx
deepa63690
 
PPTX
File handling in Python
Megha V
 
PPTX
ReadingWriting_CSV_files.pptx sjdjs sjbjs sjnd
ahmadalibzuwork
 
DOCX
File Handling in python.docx
manohar25689
 
PPTX
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
ShivaniJayaprakash1
 
PDF
file handling.pdf
RonitVaskar2
 
PPTX
File handling for reference class 12.pptx
PreeTVithule1
 
PDF
File handling in Python
BMS Institute of Technology and Management
 
PPTX
FILE HANDLING.pptx
kendriyavidyalayano24
 
PPTX
files.pptx
KeerthanaM738437
 
How to process csv files
Tukaram Bhagat
 
Binary File.pptx
MasterDarsh
 
01 file handling for class use class pptx
PreeTVithule1
 
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
H file handling
missstevenson01
 
FILE HANDLING COMPUTER SCIENCE -FILES.pptx
anushasabhapathy76
 
CSV Files-1.pdf
AmitenduBikashDhusiy
 
FILE HANDLING IN PYTHON Presentation Computer Science
HargunKaurGrover
 
File handling with python class 12th .pdf
lionsconvent1234
 
file handling.pptx avlothaan pa thambi popa
senniyappanharish
 
File Handling in Python -binary files.pptx
deepa63690
 
File handling in Python
Megha V
 
ReadingWriting_CSV_files.pptx sjdjs sjbjs sjnd
ahmadalibzuwork
 
File Handling in python.docx
manohar25689
 
CBSE - Class 12 - Ch -5 -File Handling , access mode,CSV , Binary file
ShivaniJayaprakash1
 
file handling.pdf
RonitVaskar2
 
File handling for reference class 12.pptx
PreeTVithule1
 
FILE HANDLING.pptx
kendriyavidyalayano24
 
files.pptx
KeerthanaM738437
 
Ad

Recently uploaded (20)

PDF
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
PPTX
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
PPTX
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
PPTX
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
PDF
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
PDF
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
PPTX
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
How to Set Maximum Difference Odoo 18 POS
Celine George
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
CONCURSO DE POESIA “POETUFAS – PASSOS SUAVES PELO VERSO.pdf
Colégio Santa Teresinha
 
How to Convert an Opportunity into a Quotation in Odoo 18 CRM
Celine George
 
How to Manage Large Scrollbar in Odoo 18 POS
Celine George
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
Stokey: A Jewish Village by Rachel Kolsky
History of Stoke Newington
 
SPINA BIFIDA: NURSING MANAGEMENT .pptx
PRADEEP ABOTHU
 
CEREBRAL PALSY: NURSING MANAGEMENT .pdf
PRADEEP ABOTHU
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
Generative AI: it's STILL not a robot (CIJ Summer 2025)
Paul Bradshaw
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
Soil and agriculture microbiology .pptx
Keerthana Ramesh
 
The dynastic history of the Chahmana.pdf
PrachiSontakke5
 
Growth and development and milestones, factors
BHUVANESHWARI BADIGER
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
STAFF DEVELOPMENT AND WELFARE: MANAGEMENT
PRADEEP ABOTHU
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
How to Set Maximum Difference Odoo 18 POS
Celine George
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
Stereochemistry-Optical Isomerism in organic compoundsptx
Tarannum Nadaf-Mansuri
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
Ad

Data file handling in python binary & csv files

  • 1. KVS RO AGRA BINARY FILES & CSV(COMMA SEPARATED VALUES) FILES
  • 3. KVS RO AGRA CREATING BINARY FILES
  • 4. KVS RO AGRA Content of binary file which is in codes. SEEING CONTENT OF BINARY FILE
  • 5. KVS RO AGRA READING BINARY FILES TROUGH PROGRAM CONTENT OF BINARY FILE
  • 6. KVS RO AGRA PICKELING AND UNPICKLING USING PICKLE MODULE
  • 7. KVS RO AGRA PICKELING AND UNPICKLING USING PICKEL MODULE Use the python module pickle for structured data such as list or directory to a file. PICKLING refers to the process of converting the structure to a byte stream before writing to a file. while reading the contents of the file, a reverse process called UNPICKLING is used to convert the byte stream back to the original structure.
  • 9. KVS RO AGRA PICKLING AND UNPICKLING USING PICKEL MODULE Firstly we need to import the pickle module, It provides two main methods: 1) dump() method 2) load() method
  • 11. KVS RO AGRA pickle.dump() Method pickle.dump() method write the object in binary file. Syntax of dump method is: dump(object ,fileobject)
  • 12. KVS RO AGRA pickle.dump() Method # A program to write list sequence in a binary file
  • 14. KVS RO AGRA pickle.load() Method pickle.load() method is used to read the binary file. CONTENT OF BINARY FILE
  • 15. KVS RO AGRA BINARY FILE R/W OPERATION USING PICKLE MODULE import pickle Wr_file = open(r"C:UserslenovoDesktoppython filesbin1.bin", "wb") myint = 56 mylist = ["Python", "Java", "Oracle"] mystring = "Binary File Operations" mydict = { "ename": "John", "Desing": "Manager" } pickle.dump(myint, Wr_file) pickle.dump(mylist, Wr_file) pickle.dump(mystring, Wr_file) pickle.dump(mydict, Wr_file) Wr_file.close() R_file = open(r"C:UserslenovoDesktopbin1.bin", "rb") i = pickle.load(R_file) s = pickle.load(R_file) l = pickle.load(R_file) d = pickle.load(R_file) print("myint = ", I) print("mystring =", s) print("mylist = ", l) print("mydict = ", d) R_file.close()
  • 16. KVS RO AGRA READING BINARY FILE THROUGH LOOP Read objects one by one through loop import pickle Wr_file = open(r"C:UserslenovoDesktoppython filesbin1.bin", "wb") myint = 56 mylist = ["Python", "Java", "Oracle"] mystring = "Binary File Operations" mydict = { "ename": "John", "Desing": "Manager" } pickle.dump(myint, Wr_file) pickle.dump(mylist, Wr_file) pickle.dump(mystring, Wr_file) pickle.dump(mydict, Wr_file) Wr_file.close() with open(r"C:UserslenovoDesktopbin1.bin", "rb") as f: while True: try: r=pickle.load(f) print(r) print("Next item") except EOFError: break f.close()
  • 17. KVS RO AGRA INSERT/APPEND RECORD IN A BINARY FILE Here we are creating dictionary Object to dump it in a binary file import pickle Empno = int(input('Enter Employee number:')) Ename = input('Enter Employee Name:') Sal = int(input('Enter Salary')) #Creating the dictionary dict1 = {'Empno':Empno,'Name':Ename,'Salary':Sal} #Writing the Dictionary f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'ab') pickle.dump(dict1,f) f.close() f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'rb') while True: try: dict1 = pickle.load(f) print('Employee Num:',dict1['Empno']) print('Employee Name:',dict1['Name']) print('Employee Salary:',dict1['Salary']) except EOFError: break f.close()
  • 18. KVS RO AGRA SEARCH RECORD IN A BINARY FILE import pickle f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'rb') Found = False eno=int(input("Enter Employee no to be searched")) while True: try: dict1 = pickle.load(f) if dict1['Empno'] == eno: print('Employee Num:',dict1['Empno']) print('Employee Name:',dict1['Name']) print('Salary',dict1['Salary']) Found = True except EOFError: break if Found == False: print('No Records found') f.close()
  • 19. KVS RO AGRA UPDATE RECORD OF A BINARY FILE import pickle f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'rb') rec_File = [] r=int(input("enter Employee no to be updated")) m=int(input("enter new value for Salary")) while True: try: onerec = pickle.load(f) rec_File.append(onerec) except EOFError: break f.close() no_of_recs=len(rec_File) for i in range (no_of_recs): if rec_File[i]['Empno']==r: rec_File[i]['Salary'] = m f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'wb') for i in rec_File: pickle.dump(i,f) f.close()
  • 20. KVS RO AGRAimport pickle f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'rb') rec_File = [] e_req=int(input("enter Employee no to be deleted")) while True: try: onerec = pickle.load(f) rec_File.append(onerec) except EOFError: break f.close() f = open(r"C:UserslenovoDesktoppython filesEmp.dat",'wb') for i in rec_File: if i['Empno']==e_req: continue pickle.dump(i,f) f.close() DELETE RECORD OF A BINARY FILE
  • 21. KVS RO AGRA COMMA SEPARATED VALUE(CSV Files)
  • 22. KVS RO AGRA CSV FILE • CSV is a simple file format used to store tabular data, such as • a spreadsheet or database. • Files in the CSV format can be imported to and exported from programs that store data in tables, such as Microsoft Excel or OpenOffice Calc. • CSV stands for "comma-separated values“. • A comma-separated values file is a delimited text file that uses a comma to separate values. • Each line of the file is a data record. Each record consists of one or more fields, separated by commas. The use of the comma as a field separator is the source of the name for this file format
  • 23. KVS RO AGRA • One line for each record • Comma separated fields • Space-characters adjacent to commas are ignored • When data has a strict tabular structure • To transfer large database between programs • To import and export data to office applications, Qedoc modules CSV File Characteristics WHEN USE CSV?
  • 24. KVS RO AGRA • CSV is faster to handle • CSV is smaller in size • CSV is easy to generate • CSV is human readable and easy to edit manually • CSV is simple to implement and parse • CSV is processed by almost all existing applications • No standard way to represent binary data • There is no distinction between text and numeric values • Poor support of special characters and control characters • CSV allows to move most basic data only. Complex configurations cannot be imported and exported this way • Problems with importing CSV into SQL (no distinction between NULL and quotes) CSV Advantages CSV Disadvantages
  • 25. KVS RO AGRA CSV file handling in Python To perform read and write operation with CSV file, • we must importcsv module. • open() function is used toopen file, and return file object.
  • 26. KVS RO AGRA WRITING DATA IN CSV FILE  import csv module  Use open() to open CSV file by specifying mode “w” or “a”, it will return file object.  “w” will overwrite previous content  “a” will add content to the end of previous content.  Pass the file object to writer object with delimiter.  Then use writerow() to send data in CSV file
  • 27. KVS RO AGRA import csv with open(r'C:UserslenovoDesktoppython filesnew.csv','w') as wr: a=csv.writer(wr,delimiter=",") a.writerow(["Roll no","Name","Marks"]) a.writerow(["1","Rahul","85"]) a.writerow(["2","Priya","80"]) wr.close() Writing to CSV file
  • 28. KVS RO AGRA Content of CSV file
  • 29. KVS RO AGRA Reading from CSV file • import csv module • Use open() to open csv file, it will return file object. • Pass this file object to reader object. • Perform operation you want
  • 30. KVS RO AGRA import csv with open(r'C:UserslenovoDesktoppython filesnew.csv',‘r') as rr: a=csv.reader(rr) for i in a: print(i) wr.close() Reading from CSV file
  • 31. KVS RO AGRA THANK YOU & HAVE A NICE DAY UNDER THE GUIDANCE OF KVS RO AGRA VEDIO LESSON PREPARED BY: KIRTI GUPTA PGT(CS) KV NTPC DADRI