0% found this document useful (0 votes)
6 views35 pages

10 B Python CSV File

Uploaded by

Pinku Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views35 pages

10 B Python CSV File

Uploaded by

Pinku Sharma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

File Handling

in Python
csv
files
Comma
Separated
Values
Creating csv files

import csv
x=open("abc.csv","
w")
x.close()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("abc.csv","w"
)
y=csv.writer(x)
z=(1,"amit",10)
y.writerow(z)
x.close()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("abc.csv","w")
y=csv.writer(x)
z=(1,"amit",10)
y.writerow(z)
z=(2,"sumit",11)
y.writerow(z)
z=(3,"ajay",12)
y.writerow(z)
z=(4,"sanjay",13)
y.writerow(z)
x.close()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("abc.csv","w")
y=csv.writer(x)
z=[1,"amit",10]
y.writerow(z)
z=[2,"sumit",11]
y.writerow(z)
z=[3,"ajay",12]
y.writerow(z)
z=[4,"sanjay",13]
y.writerow(z)
x.close()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("abc.csv","w")
y=csv.writer(x)
z=[1,"amit",10]
y.writerow(z)
z=(2,"sumit",11)
y.writerow(z)
z=[3,"ajay",12]
y.writerow(z)
z=(4,"sanjay",13)
y.writerow(z)
x.close()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("abc.csv","w")
y=csv.writer(x)
y.writerow(["Roll","Name","Marks"])
z=[1,"amit",10]
y.writerow(z)
z=(2,"sumit",11)
y.writerow(z)
z=[3,"ajay",12]
y.writerow(z)
z=(4,"sanjay",13)
y.writerow(z)
x.close()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open(“abc.csv”, “w” ,newline=‘’)
y=csv.writer(x)
y.writerow(["Roll","Name","Marks"])
z=[1,"amit",10]
y.writerow(z)
z=(2,"sumit",11)
y.writerow(z)
z=[3,"ajay",12]
y.writerow(z)
z=(4,"sanjay",13)
y.writerow(z)
x.close()
Contents of csv files
in Notepad as well as
in Excel
Creating csv files
import csv
x=open("abc.csv","w",newline="")
y=csv.writer(x)
y.writerow(["Roll","Name","Marks"])
LIST=[]
for m in range(3):
r=int(input("Roll Please "))
n=input("Name Please ")
m=int(input("Marks Please "))
LIST.append([r,n,m])
y.writerows(LIST)
x.close()
Reading csv files
import csv
x=open("abc.csv","r")
y=csv.reader(x)
for z in y:
print(z)
x.close()
Output
Reading csv files
import csv
x=open("abc.csv","r")
y=csv.reader(x)
for z in y:
print(z[0],'\t',z[1],'\
t',z[2])
x.close()
Output
Records
import csv
x=open("abc.csv","r")
y=csv.reader(x)
a=input("Enter marks")
for z in y:
if z[2]==a:
print(z[0],'\t',z[1],'\
t',z[2])
x.close()
Output
Reading Selected
Records
import csv
x=open("abc.csv","r")
y=csv.reader(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
x.close()
O
R
Reading Selected
Records
import csv
x=open("abc.csv","r")
y=csv.reader(x)
next(y)
for z in y:
if int(z[2])>15:
print(z[0],'\t',z[1],'\t',z[2])
x.close()
next() moves the file pointer to
next record
O
R
Records
import csv
x=open("abc.csv","r")
y=csv.reader(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])
x.close()
Output
Updating Records
import csv
op=open("abc.csv","r")
dt=csv.reader(op)
up_dt=[]
for r in dt:
if r[0]=='2':
r[2]='100'
up_dt.append(r)
print(up_dt)
op.close()
op=open("abc.csv","w",newline='')
dt=csv.writer(op)
dt.writerows(up_dt)
op.close()
Output
Deleting Records
import csv
x=open("abc.csv","w",newl
ine='')
y=csv.writer(x)
b=[[1,"Amit",10],[2,"Sumit",20],[3,"Ajay",30],
[4,"Sanjay",40]]

y.writerows(b)
x.close()
Deleting Records
import csv
x=open("abc.csv","r")
y=csv.reader(x)
z=input("Enter roll to delete ")
b=[]
for a in y:
if a[0]!=z:
b.append(a)
x.close()
x=open("abc.csv","w",newline='')
y=csv.writer(x)
y.writerows(b)
x.close()
Output

You might also like