Wednesday, July 15, 2020

CSV File Practice Python File Handling


csvfile
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()
['sno', 'name', 'class']
['1', 'ovendra', '12']
['2', 'sukhveer', '12']
['3', 'krishnakant', '12']
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()
sno
1
2
3
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()
sno name
1 ovendra
2 sukhveer
3 krishnakant
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()
sno name class
1 ovendra 12
2 sukhveer 12
3 krishnakant 12
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()
class name sno
12 ovendra 1
12 sukhveer 2
12 krishnakant 3
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()
[['sno', 'name', 'class'], ['1', 'ovendra', '12'], ['2', 'sukhveer', '12'], ['3', 'krishnakant', '12']]
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()
first row ['sno', 'name', 'class']
last row ['3', 'krishnakant', '12']
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()
1
2
3
4
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()
sno,name,class
1,ovendra,12
2,sukhveer,12
3,krishnakant,12
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()
sno:name:class
1:ovendra:12
2:sukhveer:12
3:krishnakant:12
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()
enter name to be searchedsukhveer
roll no  2 name sukhveer class 12
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()
enter day number ,0 for exit1
enter name of daysun
enter day number ,0 for exit2
enter name of daymon
enter day number ,0 for exit3
enter name of daytue
enter day number ,0 for exit4
enter name of daywed
enter day number ,0 for exit0
enter name of day0
In [32]:
import csv
f=open('studnew.csv','r')
readobj=csv.reader(f)
for row in readobj:
    print(row)
f.close()
['dayno', 'dayname']
['1', 'sun']
['2', 'mon']
['3', 'tue']
['4', 'wed']
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)
['1', 'china']
[]
['2', 'india']
[]
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)
['sno', 'sub']
[]
['1', 'phy']
[]
['2', 'chem']
[]
['3', 'math']
[]
['4', 'english']
[]
['5', 'cs']
[]
In [ ]:
 
In [2]:
import csv
file=open('prot.csv','r')
reader = csv.reader(file)
for row in reader:
        print(row)
['SN', 'Movie', 'Protagonist']
['1', 'Lord of the Rings', 'Frodo Baggins']
['2', 'Harry Potter', 'Harry Potter']
In [6]:
import csv
with open('prot.csv', 'r',) as file:
    reader = csv.reader(file, delimiter = '\t')
    for row in reader:
        print(row)
['SN,Movie,Protagonist']
['1,Lord of the Rings,Frodo Baggins']
['2,Harry Potter,Harry Potter']
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))
{'SN': '1', 'Movie': 'Lord of the Rings', 'Protagonist': 'Frodo Baggins'}
{'SN': '2', 'Movie': 'Harry Potter', 'Protagonist': 'Harry Potter'}
In [ ]:
 

No comments:

Post a Comment