Q. A file sports.dat contains information in following format :

Event ~ Participant

Write a function that would read contents from file sports.dat and creates a file named Atheletic.dat copying only those records from sports.dat where the event name is "Atheletics".


You can understand by Watching video :-



Answer :-

Binary File Program :-

import pickle
def  portal(  ) :
    file1 = open("sports.dat","rb")
    file2 = open("Atheletics.dat","wb")
    try :
        while True :
            data = pickle.load( file1 )
            word = data.split(" ~ ")
            if data [ : 9 ] == "atheletic" or data [ : 9 ] == "Atheletic":
                pickle.dump( data, file2 )
    except EOFError :
        file1.close()
        file2.close()
    print("Pragram Run Succesfully !!")
portal()

Text File Program :-

def  portal(  ) :
    file1 = open("sports.txt","r")
    file2 = open("Atheletics.txt","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()
    
portal()

We write Data in sports.dat file by following script

import pickle
f = open( "sports.dat","wb" )
pickle.dump("Football ~ Path",f)
pickle.dump("Atheletics ~ Walla",f)
pickle.dump("Cricket ~ Portal",f)
pickle.dump("Hockey ~ Express",f)
pickle.dump("Atheletics ~ Python",f)
pickle.dump("Chess ~ Computer",f)
f.close()

sports.dat file contain :- ( in Binary Language)

€•       Å’Football ~ Path”.€•       Å’Atheletics ~ Walla”.€•       Å’Cricket ~ Portal”.€•       Å’Hockey ~ Express”.€•       Å’Atheletics ~ Python”.€•       Å’Chess ~ Computer”.

Output :-

Program Run Successfully !!
>>> 

Atheletic.dat file Contain :- ( in Binary Language )

€•       Å’Atheletics ~ Walla”.€•       Å’Atheletics ~ Python”.




14 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

  1. It's supposed to be a binary file. Why is it a text file?

    ReplyDelete
    Replies
    1. If it is working in txt file then why i will take binary file : )

      Delete
  2. The question is in binary file right ? .dat..
    So if possible please upload in binary file

    ReplyDelete
  3. Why 9 no. of terms are considered, their can be 'n' no. of terms

    ReplyDelete
  4. You are suppose to use pickle module, load and dump, how just changing the extension from txt to dat will help? Correction required.

    ReplyDelete
  5. I am getting invalid load key for pickle error
    line 7, in portal
    data = pickle.load( file1 )
    _pickle.UnpicklingError: invalid load key, 'A'.

    ReplyDelete
  6. what is the purpose of splitting contents in data and storing it in 'words' if we are not using 'words' afterwards?

    ReplyDelete
  7. hi sagar what sudden surprise !!!!!

    ReplyDelete
    Replies
    1. hi harisath what a sudden surprise!!

      Delete
  8. hi harisath what a sudden surprise!!

    ReplyDelete
    Replies
    1. aagayam thee pidithal nila thoonguma!!

      Delete

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post