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”.
It's supposed to be a binary file. Why is it a text file?
ReplyDeleteIf it is working in txt file then why i will take binary file : )
DeleteThe question is in binary file right ? .dat..
ReplyDeleteSo if possible please upload in binary file
You can use .dat file as txt file ... : )
DeleteWhy 9 no. of terms are considered, their can be 'n' no. of terms
ReplyDeleteWatch this video to understand https://www.youtube.com/watch?v=o_wSsbM3Ljk&t=142s
DeleteYou are suppose to use pickle module, load and dump, how just changing the extension from txt to dat will help? Correction required.
ReplyDeleteI am getting invalid load key for pickle error
ReplyDeleteline 7, in portal
data = pickle.load( file1 )
_pickle.UnpicklingError: invalid load key, 'A'.
IPlease import pickle module.
Deletewhat is the purpose of splitting contents in data and storing it in 'words' if we are not using 'words' afterwards?
ReplyDeletehi sagar what sudden surprise !!!!!
ReplyDeletehi harisath what a sudden surprise!!
Deletehi harisath what a sudden surprise!!
ReplyDeleteaagayam thee pidithal nila thoonguma!!
DeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )