File Handling
in Python
csv
files
Comma
Separated
Values
Creating csv files
import csv
x=open("[Link]","
w")
[Link]()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("[Link]","w"
)
y=[Link](x)
z=(1,"amit",10)
[Link](z)
[Link]()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("[Link]","w")
y=[Link](x)
z=(1,"amit",10)
[Link](z)
z=(2,"sumit",11)
[Link](z)
z=(3,"ajay",12)
[Link](z)
z=(4,"sanjay",13)
[Link](z)
[Link]()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("[Link]","w")
y=[Link](x)
z=[1,"amit",10]
[Link](z)
z=[2,"sumit",11]
[Link](z)
z=[3,"ajay",12]
[Link](z)
z=[4,"sanjay",13]
[Link](z)
[Link]()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("[Link]","w")
y=[Link](x)
z=[1,"amit",10]
[Link](z)
z=(2,"sumit",11)
[Link](z)
z=[3,"ajay",12]
[Link](z)
z=(4,"sanjay",13)
[Link](z)
[Link]()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("[Link]","w")
y=[Link](x)
[Link](["Roll","Name","Marks"])
z=[1,"amit",10]
[Link](z)
z=(2,"sumit",11)
[Link](z)
z=[3,"ajay",12]
[Link](z)
z=(4,"sanjay",13)
[Link](z)
[Link]()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open(“[Link]”, “w” ,newline=‘’)
y=[Link](x)
[Link](["Roll","Name","Marks"])
z=[1,"amit",10]
[Link](z)
z=(2,"sumit",11)
[Link](z)
z=[3,"ajay",12]
[Link](z)
z=(4,"sanjay",13)
[Link](z)
[Link]()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("[Link]","w",newline="")
y=[Link](x)
[Link](["Roll","Name","Marks"])
LIST=[]
for m in range(3):
r=int(input("Roll Please "))
n=input("Name Please ")
m=int(input("Marks Please "))
[Link]([r,n,m])
[Link](LIST)
[Link]()
Reading csv files
import csv
x=open("[Link]","r")
y=[Link](x)
for z in y:
print(z)
[Link]()
Output
Reading csv files
import csv
x=open("[Link]","r")
y=[Link](x)
for z in y:
print(z[0],'\t',z[1],'\
t',z[2])
[Link]()
Output
Records
import csv
x=open("[Link]","r")
y=[Link](x)
a=input("Enter marks")
for z in y:
if z[2]==a:
print(z[0],'\t',z[1],'\
t',z[2])
[Link]()
Output
Reading Selected
Records
import csv
x=open("[Link]","r")
y=[Link](x)
a=0
for z in y:
if a>0:
if int(z[2])>15:
print(z[0],'\t',z[1],'\t',z[2])
a=a+1
[Link]()
O
R
Reading Selected
Records
import csv
x=open("[Link]","r")
y=[Link](x)
next(y)
for z in y:
if int(z[2])>15:
print(z[0],'\t',z[1],'\t',z[2])
[Link]()
next() moves the file pointer to
next record
O
R
Records
import csv
x=open("[Link]","r")
y=[Link](x)
for z in y: Reads one
record
break and loop
breaks
for z in y:
if int(z[2])>15:
print(z[0],'\t',z[1],'\t',z[2])
[Link]()
Output
Updating Records
import csv
op=open("[Link]","r")
dt=[Link](op)
up_dt=[]
for r in dt:
if r[0]=='2':
r[2]='100'
up_dt.append(r)
print(up_dt)
[Link]()
op=open("[Link]","w",newline='')
dt=[Link](op)
[Link](up_dt)
[Link]()
Output
Deleting Records
import csv
x=open("[Link]","w",newl
ine='')
y=[Link](x)
b=[[1,"Amit",10],[2,"Sumit",20],[3,"Ajay",30],
[4,"Sanjay",40]]
[Link](b)
[Link]()
Deleting Records
import csv
x=open("[Link]","r")
y=[Link](x)
z=input("Enter roll to delete ")
b=[]
for a in y:
if a[0]!=z:
[Link](a)
[Link]()
x=open("[Link]","w",newline='')
y=[Link](x)
[Link](b)
[Link]()
Output