[go: up one dir, main page]

0% found this document useful (0 votes)
121 views6 pages

Data File Handling - Worksheet 1 - 3 Marks

The document is a worksheet for Grade 12 Computer Science focusing on Data File Handling, containing various programming tasks. It includes functions to read text files, count specific words, vowels, and lines, and manipulate file content in Python. Each task is designed to enhance students' understanding of file handling and string manipulation in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
121 views6 pages

Data File Handling - Worksheet 1 - 3 Marks

The document is a worksheet for Grade 12 Computer Science focusing on Data File Handling, containing various programming tasks. It includes functions to read text files, count specific words, vowels, and lines, and manipulate file content in Python. Each task is designed to enhance students' understanding of file handling and string manipulation in programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Grade: 12 Computer Science

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)

Data File Handling Worksheet – 1 - 3 Marks Page 1


6. Write a function DISPLAYWORDS( ) in python to display the count of words starting
with “t” or “T” in a text file ‘STORY.TXT’.

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

Data File Handling Worksheet – 1 - 3 Marks Page 2


11. Write a method/function DISPLAYWORDS() in python to read lines from a text file
STORY.TXT, and display those words, which are less than 4 characters.
12. 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.
13. Write a method COUNTLINES() in Python to read lines from text file ‘TESTFILE.TXT’
and display the lines which are starting with any article (a, an, the) insensitive of the
case.
Example: If the file content is as follows:
Give what you want to get.
We all pray for everyone’s safety.
A marked difference will come in our country.
The Prime Minister is doing amazing things.
The COUNTLINES() function should display the output as:
The number of lines starting with any article are : 2
14. Write a function GPCount() in Python, which should read each character of a text file
“STORY.TXT” and then count and display the count of occurrenceof alphabets G and P
individually (including small cases g and p too).
Example: If the file content is as follows:
God helps those who help themselves.
Great way to be happy is to remain positive.
Punctuality is a great virtue.
The GPCount() function should display the output as:
The number of G or g: 3
The number of P or p : 6
15. Write a method count_words_e()in Python to read the content of a textfile and count
the number of words ending with 'e' in the file.
Example: If the file content is as follows:
An apple a day keeps the doctor away.
We all pray for everyone’s safety.
A marked difference will come in our country.

Data File Handling Worksheet – 1 - 3 Marks Page 3


The count_words_e() function should display the output as:
No. of such words: 4
16. Write a function reverseFile()in Python, which should read the content of a text file
“TESTFILE.TXT” and display all its line in the reverse order.
Example: If the file content is as follows:
It rained yesterday.
It might rain today.
I wish it rains tomorrow too.
I love Rain.
The RainCount()function should display the output as:
.yadretseydeniartI
.yadotniarthgimtI
.ootworromotsniartihsiw I
.niaRevol I
17. Write a method/function COUNT_BLANK_SPACES() in Python to read lines from a
text file STORY.TXT, and display the count of blank spaces in the text file.
18. Write a method/function DISPLAYWORDS() in python to read lines from a text file
POEM.TXT, and display those words, which are less than 4 characters
19. Write a function in python to count the number of lines in a text file ‘Country.txt’
which are starting with an alphabet ‘W’ or ‘H’.
For example, If the file contents are as follows:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.
The output of the function should be:
W or w : 1
H or h : 2
20. Write a user defined function to display the total number of words present in a text
file 'Quotes.txt'
For example if the file contents are as follows:

Data File Handling Worksheet – 1 - 3 Marks Page 4


Living a life you can be proud of doing your best Spending your time with people and
activities that are important to you Standing up for things that are right even when it’s
hard Becoming the best version of you.
The countwords() function should display the output as:
Total number of words : 40
21. Write a function COUNTLINES( ) which reads a text file STORY.TXT and then count
and display the number of the lines which starts and ends with same letter irrespective
of its case .
For example if the content of the text file STORY.TXT is :
The person has a sent a lovely tweet
Boy standing at station is very disturbed
Even when there is no light we can see
How lovely is the situation
The expected output is :
The Number of lines starting with same letter is 2
22. Write a function VOWEL_WORDS which reads a text file TESTFILE.TXT and then
count and display the number of words starting with vowels ‘a’ or ‘u’ (including capital
cases A and U too)
For example is the text in the file TESTFILE.txt is :
The train from Andaman has earned the name ‘Floating Train’. What is so unique about
this train to receive such a name?
The expected output is :
The Number of words starting with letter ‘a’ is : 3
The Number of words starting with letter ‘u’ is : 1
23. Define a function SHOWWORD () in python to read lines from a text file STORY.TXT,
and display those words, whose length is less than 5.
24. Write a user defined function in python that displays the number of lines starting
with 'H' in the file para.txt
25. Write a function in python to count the number lines in a text file ‘Country.txt’ which
is starting with an alphabet ‘W’ or ‘H’. If the file contents are as follows:
Whose woods these are I think I know.
His house is in the village though;
He will not see me stopping here
To watch his woods fill up with snow.

Data File Handling Worksheet – 1 - 3 Marks Page 5


The output of the function should be:
W or w : 1
H or h : 2

Data File Handling Worksheet – 1 - 3 Marks Page 6

You might also like