Example 1: Write a Python program to create a file containing student records where each record contain rollno and marks in 3 subjects separated by a comma (marks to be considered as list of 3 values).
##Write a Python program to create a file containing student records where each record contain rollno and marks in 3 subjects separated by a comma (marks to be considered as list of 3 values). e.g. records of students: 1, [45, 40, 35], 2, [41, 38, 39], 3, [35, 30, 37] (each line of the file containing record of only 1 student). Prepare mark list in the following format:
##Roll No. Mark-1 Mark-2 Mark-3 Total
## 1 45 40 35 120
stud = {}
mrk = []
print("ENTER ZERO NUMBER FOR EXIT !!!!!!!!!!!!")
print("ENTER STUDENT INFORMATION ------------------------ ")
while True :
rno = int(input("enter roll no.. :: -- "))
if rno == 0 :
break
else:
for i in range(3):
print("enter marks ",i+1," :: -- ",end = " ")
n = int(input())
mrk.append(n)
stud[rno] = mrk
mrk = []
print("Records are ------ ",stud)
f = open ('studFile.txt','w')
print("\nRollNo\t Mark1\t Mark2\t Mark3\t Total")
f.write("RollNo\t Mark1\t Mark2\t Mark3\t Total \n")
tot = 0
for r in stud:
print(r,"\t",end=" ")
f.write(str(r) + "\t" )
for m in stud[r] :
tot = tot + m
print(m,"\t",end=" ")
f.write(str(m) + "\t" )
print(tot)
f.write(str(tot) + '\n')
tot = 0
f = open ('studFile.txt')
print(f.read())
Example 2: Write a Python program to create a file containing student records where each record contain rollno and marks in 3 subjects separated by a comma (marks to be considered as list of 3 values).
##Write a Python program to create a file of elements of any data type (mixed data type, i.e. some elements maybe of type int, some elements of type float and some elements of type string). Split this file into three file containing elements of same data type (i.e. 1st file of integers only, 2nd file of float only and 3rd file of strings only). Take input from the user to create the file.
f = open('MixedFile.txt','w')
while True :
user = input("Enter Any Data Type Element :: ")
if user == 'end':
print('!!!!!!!! EXIT !!!!!!!!!!!!')
break
else :
f.write(user + '\n')
f.close()
f = open('MixedFile.txt')
a = []
a = f.read().split()
f.close()
fs = open ('StringFile.txt','w')
ff = open ('FloatFile.txt','w')
fn = open ('NumberFile.txt','w')
for i in a :
try:
int(i)
fn.write(i + '\n')
except:
try:
float(i)
ff.write(i + '\n')
except:
fs.write(i + '\n')
f.close()
fs.close()
fn.close()
ff.close()
print("reading................")
fs = open ('StringFile.txt','r')
ff = open ('FloatFile.txt','r')
fn = open ('NumberFile.txt','r')
print(fs.read())
print(fn.read())
print(ff.read())
Example 3: Write a Python program to create a file containing student records where each record contain rollno and marks in 3 subjects separated by a comma (marks to be considered as list of 3 values).
##Write a Python program to create a file of strings by taking input from the user and then create a dictionary containing each string along with their frequencies. (e.g. if the file contains ‘apple’, ‘banana’, ‘fig’, ‘apple’, ‘fig’, ‘banana’, ‘grapes’, ‘fig’, ‘grapes’, ‘apple’ then the output should be {'apple': 3, 'banana': 2, 'fig': 3, 'grapes': 2}.
lst = []
d = dict()
print("ENTER ZERO NUMBER FOR EXIT !!!!!!!!!!!!")
while True:
user = input('enter string element :: -- ')
if user == "0":
break
else:
lst.append(user)
print("LIST ELEMENR ARE :: ",lst)
l = len(lst)
for i in range(l) :
c = 0
for j in range(l) :
if lst[i] == lst[j ]:
c += 1
d[lst[i]] = c
print("dictionary is :: ",d)
f = open('FrequencyDictFile.txt','w')
f.write(str(d))
f.close()
f = open('FrequencyDictFile.txt')
print(f.read())