Professional Documents
Culture Documents
CSV FILES Online
CSV FILES Online
CSV FILES Online
t
t
h
h
e
e
Source: geeksforgeeks
Not in syllabus
reader = csv.DictReader(open('coors.csv’))
dictobj = next(reader)
C F
O
O E
F
U T
N C
R
T H H
E The function next() is used to directly
I I E
C point to this list of fields to read the
N N A next line in the CSV file. next()
O
G G D method returns the current row and
R advances the iterator to the next row
E
D
N C R
S
U O S
M L
A
B U
N
E M
D
R N
U
S
E
S
E
E
K
()
A c
l o
t u
e n
r t
n i
a n
t g
e
r
w e
a c
y o
r
o d
f s
l
i
n
e
_
n
line_num is a counter which returns the number of rows which
u have been iterated
m
c
o
u
n
t
e
r
Updating record in csv file
[in this example increased marks of all students by 2]
Updating record in csv file
import csv print(rows)
with open("fname.csv",'w') as f:
Again
#writing to file wo=csv.writer(f,lineterminator='\n')
opening file
def write_data(): wo.writerows(rows) in writing
with open("fname.csv","a") as f: mode so
wo=csv.writer(f,lineterminator='\n') #driver code that
rec=['amit',1,34] while True: previous
wo.writerow(rec) print("1. write data") contents of
data=[['sumit',2,56.5],['charu',3,56],['mohit',4,57]] print("2. update marks") the file are
wo.writerows(data) print("0. exit") truncated
and
ch=int(input("enter choice"))
updated
def upd_rec1(): if ch==1:
records are
with open("fname.csv") as f: write_data() rewritten
ro=csv.reader(f) elif ch==2: back to the
rows=[] Reading data in a list upd_rec1() file
for rec in ro: elif ch==0:
rows.append(rec) break
for i in range(1,len(rows)):
Moving through each record to increase marks by 2
rows[i][2]=float(rows[i][2])+2
D Deleting record from csv file
E
L
E
T
E
N
R A
E M
C E
O
R
D
b f
y i
l
n e
a
m
e
S
E
A
R
C
H
I
N
G
S
E
A
R
C
H
I
N
G
S
E
A
R
C
H
I
N
G
S
E
A
R
C
H
I
N
G
W
R
I
T
I
N
G
W
R
I
T
I
N
G
S
E
A
R
C
H
I
N
G
S
E
A
R
C
H
I
N
G
S
E
A
R
C
H
I
N
G
S
E
A
R
C
H
I
N
G
S
E
A
R
C
H
I
N
G
S
E
A
R
C
H
I
N
G
int(i[3])>150
Write a program to calculate the sum of all the marks given in the file “marks.csv. Records in “marks.csv” are as
follows :
Rollno, Name, Marks
1, Suman, 67
2, Aman,71
C
3, Mini, 68
A 4, Amit, 80
L
C
U
L
A
T
I
N
G
Write a program to calculate the sum of all the marks given in the file “marks.csv. Records in “marks.csv” are as
follows :
Rollno, Name, Marks
1, Suman, 67
2, Aman,71
C
3, Mini, 68
A 4, Amit, 80
L
C
def calc():
U import csv
L f=open("marks.csv","r")
A ro=csv.reader(f)
T next(f)
I s=0
N for rec in ro:
s=s + int(i[2])
G
print("Total Marks are " ,s)
f.close( )
C Write a program to copy all those records from product.csv to
o high_price.csv whose price is more than 300. Format of record
p stored in product.csv is product id, product name, price. Display
y
the records of high_price.csv
i
n
g f
i
t l
o e
o
t
h
e
r
C def copy_data():
o import csv
Write a program to copy all fr=open("product.csv" , "r")
p
those records from ro=csv.reader(fr)
y
product.csv to high_price.csv fw=open(“high_price.csv”,’w’)
i wo=csv.writer(fw)
whose price is more than
n headers=next(f)
300. Format of record stored
g f wo.writerow(headers)
in product.csv is product id, for rec in ro:
i
product name, price. Display if int(rec[2])>300:
t l
the records of high_price.csv wo.writerow(rec)
o e
fr.close( )
fw.close()
o print("product whose price is more than 300")
t fr=open("product.csv" , "r")
h ro=csv.reader(fr)
e for rec in ro:
r print(rec)
Write a program to copy all those records from product.csv to
C
o
high_price.csv whose price is more than 300. Format of record stored in
p
product.csv is product id, product name, price. Display the records of
y high_price.csv OR
i
def product():
n with open("product1.csv", "r") as f1, open("high_price.csv","w+",newline='') as f2:
g f ro=csv.reader(f1)
i wo=csv.writer(f2)
t l reclist=[]
o e for rec in ro:
if int(rec[2])>300:
reclist+=[rec]
o
wo.writerows(reclist)
t
f2.seek(0)
h ro_f2=csv.reader(f2)
e for rec in ro_f2:
r print(rec)
C
o Write a program to count number of records present in
u “data.csv” file.
n
t
i
n
g
r
e
c
o
r
d
s
C
o Write a function count_rec(fname) to count and return number
u of records present in “data.csv” file. File has headers too.
n
t def count_rec(fname):
i import csv
n f = open(fname , "r")
g ro = csv.reader(f)
next(ro) #to skip header row
r r=0
e for row in ro:
c r = r+1
o print("Number of records are " , r)
r
d
s
Write a function UPD_RENT to increase the rent by 5% of those
tenants whose tenure of stay is more than 1 year in the file
tenant.csv. Function should return the number of records
U
updated. File has the data [TID,TNAME,RENT,TENURE_OF_STAY]
p
r
d
e
a
c
t
o
e
r
d
f
s
e
w
Write a function UPD_RENT to increase the rent by 5% of those tenants whose tenure
of stay is more than 1 year in the file tenant.csv. Function should return the number of
records updated. File has the data [TID,TNAME,RENT,TENURE_OF_STAY] with headers
U def UPD_RENT(): inc=int(row[2])*0.05
p
r
import csv row[2]=int(row[2])+inc
d
e
f = open("TENANT.CSV" , "r") r = r+1
a ro = csv.reader(f) f.close()
c
t fields=next(ro) #to skip header if r!=0:
o
e row f = open("TENANT.CSV" , "w")
r
d rows=[] wo = csv.writer(f)
f for rec in ro: wo.writerow(fields)
s
e rows.append(rec) wo.writerows(rows)
w r=0 print("file updated")
for row in rows: return r
if int(row[3])>1:
Write a function DEL_REC()
to delete records of those
tenants whose tenure of
stay is less than 1 year from
the file tenant.csv. File has
the data
[TID,TNAME,RENT,TENURE_
OF_STAY]
def DEL_REC():
import csv
f1 = open("TENANT.CSV" , "r")
ro = csv.reader(f1)
Write a function DEL_REC()
fields=next(ro) #to skip header row
to delete records of those rows=[]
tenants whose tenure of for rec in ro:
rows.append(rec)
stay is less than 1 year from
f1.close()
the file tenant.csv. File has f1=open("TENANT.CSV" , "w“,newline=‘’)
the data wo = csv.writer(f1)
wo.writerow(fields)
[TID,TNAME,RENT,TENURE_
for row in rows:
OF_STAY] if int(row[3])>=1:
wo.writerow(row)
print("file updated")
DEL_REC()
C. csv