Data File Handling - Worksheet 1 - 3 Marks
Data File Handling - Worksheet 1 - 3 Marks
1. Write a function countINDIA() which read a text file ‘myfile.txt’ and print the
frequency
of the words ‘India’ in it (ignoring case of the word).
Example: If the file content is as follows:
INDIA is my country.
I live in India. India has many states.
The countIndia() function should display the output as:
Frequency of India is 3
2. Write a function countVowel() in Python, which should read each character of a text
file“myfile.txt” and then count and display the count of occurrence of vowels (including
small cases and upper case).
Example:
If the file content is as follows:
INDIA is my country. I live in India. India has many states.
The countVowel() function should display the output as:
Total number of vowels are : 20
3. A pre-existing text file data.txt has some words written in it. Write a python function
displaywords() that will print all the words that are having length greater than 3.
Example: For the file content:
A man always wants to strive higher in his life
He wants to be perfect.
The output after executing displayword() will be:
Always wants strive higher life wants perfect
4. A pre-existing text file info.txt has some text written in it. Write a python function
countvowel() that reads the contents of the file and counts the occurrence of
vowels(A,E,I,O,U) in the file.
5. Write a function COUNT_AND( ) in Python to read the text file “STORY.TXT” and count
the number of times “AND” occurs in the file. (include AND/and/And in the counting)
7. What is the advantage of using a csv file for permanent storage? 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’.
8. 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.
9. Write a function in Phyton 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
10. 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