Programming Examples
Python program to create a function that would read contents from the sports.dat and creates a file named Atheletic.dat
A file sports.dat contains information in the following format : Event ~ Participant
Write a function that would read contents from the sports.dat and creates a file named Atheletic.dat copying only those records from sports.dat where the event name is "Atheletics".
Solution
def filter( ) :
file1 = open("sports.dat","r")
file2 = open("Atheletics.dat","w")
lst = file1.readlines()
for i in lst :
print(i [ : 9 ])
if i [ : 9 ] == "atheletic" or i [ : 9 ] == "Atheletic" :
file2.write(i)
file1.close()
file2.close()
filter()
Output