Data File Handling
Data File Handling
Question1:
Write a function in Python that counts the number of “Me” or “My” words present in a text file
“STORY.TXT”.
If the “STORY.TXT” contents are as follows:
My first book was Me and My Family.
It gave me chance to be Known to the world.
The output of the function should be: Count of Me/My in file: 4
Question2:
Write a function AMCount() in Python, which should read each character of a text file
STORY.TXT, should count and display the occurance of alphabets A and M (including small
cases a and m too).
Example:
If the file content is as follows:
Updated information
As simplified by official websites.
The EUCount() function should display the output as:
A or a:4
M or m :2
Question 3:
A binary file “Book.dat” has structure [BookNo, Book_Name, Author, Price].
i. Write a user defined function CreateFile() to input data for a record and add to Book.dat .
ii. Write a function CountRec(Author) in Python which accepts the
Author name as parameter and count and return number of books by the given Author are stored in
the binary file “Book.dat”
Question 4:
A binary file “STUDENT.DAT” has structure (admission_number, Name, Percentage). Write a
function countrec() in Python that would read contents of the file “STUDENT.DAT” and display the
details of those students whose percentage is above 75. Also display number of students scoring
above 75%.
Question 5:
Write a method COUNTLINES() in Python to read lines from text file ‘TESTFILE.TXT’ and display
the lines which are not starting with any vowel.
Example:
If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
pg. 1 GA
A marked difference will come in our country.
pg. 2 GA
fout=open("_____________") #Statement 2
found=False
eid=int(input("Enter employee id to update their salary :: "))
while True:
try:
rec=______________ #Statement 3
if rec["Employee id"]==eid:
found=True
rec["Salary"]=int(input("Enter new salary
:: "))
pickle.____________ #Statement 4
else:
pickle.dump(rec,fout)
except:
break
if found==True:
print("The salary of employee id ",eid," has been updated.")
else:
print("No employee with such id is not found")
fin.close()
fout.close()
(i) Which module should be imported in the program? (Statement 1)
(ii) Write the correct statement required to open a temporary file named temp.dat. (Statement 2)
(iii) Which statement should Aman fill in Statement 3
(iv) To read the data from the binary file, record.dat and in Statement 4 to write the updated data in
the file, temp.dat?
Question 10:
Write a function in Python to read a text file, Alpha.txt and displays those lines which begin with the
word ‘You’.
Question 11:
Write a function, vowelCount() in Python that counts and displays the number of vowels in the text
file named Poem.txt.
Question 12:
Vedansh is a Python programmer working in a school. For the Annual Sports Event, he has
created a csv file named Result.csv, to store the results of students in different sports events. The
structure of Result.csv is :
[St_Id, St_Name, Game_Name, Result]
Where
pg. 3 GA
St_Id is Student ID (integer)
ST_name is Student Name (string)
Game_Name is name of game in which student is participating(string)
Result is result of the game whose value can be either 'Won', 'Lost' or 'Tie'
For efficiently maintaining data of the event, Vedansh wants to write the following user defined
functions:
Accept() – to accept a record from the user and add it to the file Result.csv. The column headings
should also be added on top of the csv file.
wonCount() – to count the number of students who have won any event.
As a Python expert, help him complete the task.
Question 13:
Consider a file, SPORT.DAT, containing records of the following structure:
[SportName, TeamName, No_Players]
Write a function, copyData(), that reads contents from the file SPORT.DAT and copies the records
with Sport name as “Basket Ball” to the file named BASKET.DAT. The function should return the
total number of records copied to the file BASKET.DAT.
Question 14:
A Binary file, CINEMA.DAT has the following structure:
{MNO:[MNAME, MTYPE]}
Where
MNO – Movie Number
MNAME – Movie Name
MTYPE is Movie Type
Write a user defined function, findType(mtype), that accepts mtype as parameter and displays all the
records from the binary file CINEMA.DAT, that have the value of Movie Type as mtype.
Question 15:
Write a user-defined function in Python named showInLines() which reads contents of a text file
named STORY.TXT and displays every sentence in a separate line.
Assume that a sentence ends with a full stop (.), a question mark (?), or an exclamation mark (!).For
example, if the content of file STORY.TXT is as follows:
Our parents told us that we must eat vegetables to be healthy. And it turns out, our parents
were right! So, what else did our parents tell?
Question 16:
pg. 4 GA
Write a function, c_words() in Python that separately counts and displays the number of
uppercase and lowercase alphabets in a text file, Words.txt.
Question 17:
Atharva is a programmer, who has recently been given a task to write a Python code to perform the
following binary file operation with the help of a user defined function/module :
Copy_new() : to create a binary file new_items.dat and write all the item details stored in the binary
file, items.dat, except for the item whose item_id is 101. The data is stored in the following format :
{item_id:[item_name,amount]}
pg. 5 GA
Emp_Id : Employee id
Name : Employee Name
Salary : Employee Salary
Write a user-defined function, `disp_Detail()`, that would read the contents of the file `EMP.DAT`
and display the details of those employees whose salary is below 25000.
Question 20:
Write a method/function COUNTWORDS() in Python to read contents from a text file
DECODE.TXT, to count and return the occurrence of those words, which are having 5 or more
characters.
Question 21:
Write a method/function COUNTLINES() in Python to read lines from a text file
ONTENT.TXT, and display those lines, which have @ anywhere in the line.
For example :
If the content of the file is :
Had an amazing time at the concert last night with @MusicLoversCrew.
Excited to announce the launch of our new website!G20 @ India
The method/function should display
Had an amazing time at the concert last night with
@MusicLoversCrew
G20 @ India
Question 22:
Mr. Mahesh is a Python Programmer working in a school. He has to maintain the records of the
sports students. He has created a csv file named sports.csv, to store the details. The structure of
sports.csv is :
[sport_id, competition, prize_won]
where
sport_id, is Sport id (integer)
competition is competition name (string)
prize_won is (“Gold”, “Silver”, “Bronze”)
Mr. Mahesh wants to write the following user-defined functions :
Add_detail(): to accept the detail of a student and add to a csv file,"sports.csv".
Count_Medal(): to display the name of competitions in which students have won "Gold" medal.
Help him in writing the code of both the functions.
Question 23:
A Binary file, "Items.dat" has the following structure :
[Icode, Description, Price]
Where
Icode – Item code
Description – Detail of item
Price – Price of item
Write a function Add_data(), that takes Icode,
Description and Price from the user and writes the
information in the binary file "Items.dat".
*********
pg. 6 GA