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 ‘record.csv’.
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 ‘record.csv’.
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 ‘furdata.csv’.
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 visiors.txt, 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 book.txt, 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 "Story.txt" 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=csv.writer (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 ("record.csv", "r",newline="\n")
data=csv.reader (fin)
for i in data:
print(i)
fin.close ()
ADD ()
COUNTR ()
2.
import csv
def add():
fout=open("furdata.csv", "a", newline='\n')
wr=csv.writer(fout)
fid=int(input("Enter Furniture Id :: "))
fname=input("Enter Furniture name :: ")
fprice=int(input("Enter price :: "))
FD=[fid, fname, fprice]
wr.writerow(FD)
fout. close()
def search():
fin=open("furdata.csv", "r", newline='\n')
data=csv.reader(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 = line.strip() # Remove leading/trailing whitespace
if line.startswith('p') or line.startswith('P'):
print(line)
displaystarting_with_p('visitors.txt')
4.
def count_word(file, word):
count = 0
with open(file_path, 'r') as file:
for line in file:
words = line.split()
count += words.count(word)
print(f"The word '{word}' occurs {count} times in the file.")
file= 'book.txt'
count_word(file, 'are')
5.
def revtext():
f=open("Story.txt".'r')
s=' '
while True:
d=f.readline()
if not d:
break
else:
m=d.split()
for i in m:
if i[0]=='i' or i[0]=='I':
s=s+' '+(i[::-1])
else:
s=s+' '+i
print(s)
s=' '
revtext()