In [8]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(row)
f.close()
In [9]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(row[0])
f.close()
In [10]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(row[0],row[1])
f.close()
In [11]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(row[0],row[1],row[2])
f.close()
In [12]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(row[2],row[1],row[0])
f.close()
In [13]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
rows=[]
for row in readobj:
rows.append(row)
print(rows)
f.close()
In [16]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
rows=[]
for row in readobj:
rows.append(row)
l=len(rows)
print('first row',rows[0])
print('last row',rows[l-1])
f.close()
In [17]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(readobj.line_num)
f.close()
In [18]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(','.join(row))
f.close()
In [ ]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(';'.join(row))
f.close()
In [20]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(':'.join(row))
f.close()
In [21]:
import csv
f=open('c:\\Users\\Acer\\Desktop\\stud.csv','r')
readobj=csv.reader(f)
search=input('enter name to be searched')
for r in readobj:
if(r[1]==search):
print('roll no ',r[0] ,'name',r[1],'class',r[2])
f.close()
In [26]:
import csv
f=open('studnew.csv','w')
writ=csv.writer(f)
writ.writerow(['dayno','dayname'])
L=[1,'monday']
writ.writerow(L)
f.close()
In [31]:
import csv
f=open('studnew.csv','w',newline='')
writ=csv.writer(f,delimiter=',')
writ.writerow(['dayno','dayname'])
while True:
d=int(input('enter day number ,0 for exit'))
dn=input('enter name of day')
L=[d,dn]
if(d==0):
break
writ.writerow(L)
f.close()
In [32]:
import csv
f=open('studnew.csv','r')
readobj=csv.reader(f)
for row in readobj:
print(row)
f.close()
In [ ]:
In [ ]:
In [5]:
import csv
f=open('filcsv.csv','w',newline='')
w=csv.writer(f)
w.writerow([1,'china'])
w.writerow([2,'india'])
f.close()
In [7]:
f=open('filcsv.csv','r')
w=csv.reader(f)
for r in w:
print(r)
In [10]:
f=open('fcsv.txt','w')
w=csv.writer(f)
rs=[['sno','sub'],[1,'phy'],[2,'chem'],[3,'math'],[4,'english'],[5,'cs']]
w.writerows(rs)
In [12]:
f=open('fcsv.txt','r')
r=csv.reader(f)
for row in r:
print(row)
In [ ]:
In [2]:
import csv
file=open('prot.csv','r')
reader = csv.reader(file)
for row in reader:
print(row)
In [6]:
import csv
with open('prot.csv', 'r',) as file:
reader = csv.reader(file, delimiter = '\t')
for row in reader:
print(row)
In [3]:
import csv
with open('prot.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["SN", "Movie", "Protagonist"])
writer.writerow([1, "Lord of the Rings", "Frodo Baggins"])
writer.writerow([2, "Harry Potter", "Harry Potter"])
In [9]:
import csv
csv_rowlist = [["SN", "Movie", "Protagonist"], [1, "Lord of the Rings", "Frodo Baggins"],
[2, "Harry Potter", "Harry Potter"]]
with open('prota.csv', 'w') as file:
writer = csv.writer(file)
writer.writerows(csv_rowlist)
In [10]:
import csv
with open("prot.csv", 'r') as file:
csv_file = csv.DictReader(file)
for row in csv_file:
print(dict(row))
In [ ]: