Professional Documents
Culture Documents
Problem 1:
Write a program to read a file and count the number of occurrences of alphabets. Display the result in
sorted order of the occurrences
Program 1:
alphabet_dict = {}
for i in range (97 , 123 ):
alphabet_dict[ chr( i ) ] = 0
for i in range (65 , 91):
alphabet_dict[ chr( i ) ] = 0
fd = open('pangrams.txt','r')
ch = fd.read(1)
while ch:
if str.isalpha(ch):
alphabet_dict[ch] = alphabet_dict[ch] + 1
ch = fd.read(1)
alpha_nze_dict = { k : v for (k , v ) in
alphabet_dict.items() if v != 0}
l = sorted(alpha_nze_dict,key = alpha_nze_dict.__getitem__ )
for key in l:
print ( key , '=' , alpha_nze_dict[ key ] )
Problem 2:
Write a program to merge two file into the third file
Program 2:
fd1 = open('numlist1.txt','r')
fd2 = open('numlist2.txt','r')
fd3 = open('merlist.txt','w')
1
if line1: #checks if fd2 is empty
line = line1
fd = fd1
fd2.close()
else: # checks if fd1 is empty
line = line2
fd = fd2
fd1.close()
fd3.close()
Problem 3:
Write a program to write contents into an excel file
fd = open('sample.csv','r')
s = fd.readline()
con = 1
while con == 1:
s = input() #read inpu with comma seperation
# 98,67,77,89,90,89
fd.write( s +'\n')
con = int(input())
fd.close()
Problem 4:
Write a program to read contents from an excel file line by line
fd = open('sample.csv','r')
s = fd.readline()
print (l)
s = fd.readline()
fd.close()
2
Problem 4:
Write a program to read contents from an excel file all lines n storing the whole file as list.
fd = open('sample.csv','r')
l = fd.readlines()
print ( l )
fd.close()
Problem 5:
Write a program to read student mark details from excel file n find max min and average in
all subjects as dictionary
fd = open('m.csv','r')
sub_list = [ [ ] for i in range(6) ]
#print (sub_list)
l = fd.readlines()
print ( l )
for dataString in l:
dl = dataString.split(',')
for i in range ( 6):
sub_list[ i ].append(int(dl[ i ]))
#print (sub_list)
list_subjects = [ 'English','Maths','Science','SST','VE','CS' ]
fd.close()