Data File Handling
1. Write a Program in Python that defines and calls the following user
defined functions:
(i) ADD() – To accept and add data of an employee to a CSV file ‘[Link]’.
Each record consists of a list with field elements as empid, name and
mobile to store employee id, employee name and employee salary
respectively.
(ii) COUNTR() – To count the number of records present in the
CSV file named ‘[Link]’.
2. Give any one point of difference between a binary file and a csv file.
Write a Program in Python that defines and calls the following user defined
functions:
(i) add() – To accept and add data of an employee to a CSV file ‘[Link]’.
Each record consists of a list with field elements as fid, fname and fprice to
store furniture id, furniture name and furniture price respectively.
(ii) search()- To display the records of the furniture whose price is more than
10000.
3. Write a function in Python to read lines from a text file [Link], and
display only those lines, which are starting with an alphabet 'P'.
If the contents of file is :
Visitors from various cities are
coming here. Particularly, they
want to visit the museum.
Looking to learn more history about countries with their cultures.
The output should be:
Particularly, they want to visit the museum
4. Write a method in Python to read lines from a text file [Link], to find
and display the occurrence of the word 'are'.
For example, if the content of the file is:
Books are referred to as a man’s best friend. They are very beneficial for
mankind and have helped it evolve. Books leave a deep impact on us and are
responsible for uplifting our mood.
The output should be
3
5. Write a function RevText() to read a text file "[Link]" and Print only
word starting with 'I' in reverse order.
Example:
If value in text file is:
INDIA IS MY COUNTRY
Output will be: AIDNI SI MY COUNTRY.
ANSWERS:
1.
import csv
def ADD () :
fout=open ("record. csv" ,"a+",newline="\n")
wr=[Link] (fout)
empid=int (input("Enter Employee id : : "))
name=input ("Enter name :: ")
mobile=int (input ("Enter mobile number :: "))
s=[empid,name, mobile]
wr. writerow(s)
fout. close ()
def COUNTR () :
fin=open ("[Link]", "r",newline="\n")
data=[Link] (fin)
for i in data:
print(i)
[Link] ()
ADD ()
COUNTR ()
2.
import csv
def add():
fout=open("[Link]", "a", newline='\n')
wr=[Link](fout)
fid=int(input("Enter Furniture Id :: "))
fname=input("Enter Furniture name :: ")
fprice=int(input("Enter price :: "))
FD=[fid, fname, fprice]
[Link](FD)
fout. close()
def search():
fin=open("[Link]", "r", newline='\n')
data=[Link](fin)
found=False
print("The Details are")
for i in data:
if int(i[2])>10000:
found=True
print(i[0],i[1], i[2])
if found == False:
print("Record not found")
fin. close()
add ()
print("Now displaying")
search()
3.
def displaystarting_with_p(file_name):
with open(file_name, 'r') as file:
for line in file:
line = [Link]() # Remove leading/trailing whitespace
if [Link]('p') or [Link]('P'):
print(line)
displaystarting_with_p('[Link]')
4.
def count_word(file, word):
count = 0
with open(file_path, 'r') as file:
for line in file:
words = [Link]()
count += [Link](word)
print(f"The word '{word}' occurs {count} times in the file.")
file= '[Link]'
count_word(file, 'are')
5.
def revtext():
f=open("[Link]".'r')
s=' '
while True:
d=[Link]()
if not d:
break
else:
m=[Link]()
for i in m:
if i[0]=='i' or i[0]=='I':
s=s+' '+(i[::-1])
else:
s=s+' '+i
print(s)
s=' '
revtext()