TEXT FILE
(Assignment)
(Assignment)
Text Files: Sequence of lines and each line is a sequence of characters (ASCII) or UNICODE characters/Each line is terminated by a special character, known as End of Line (EOL). By default the EOL will be '\n' (New line character) / Text files are stored in human readable format such as .txt, .c, ,py, .rtf et.c and they can also be created using any text editor.
A text file stores data in the form of alphabets, digits and other special symbols by storing their ASCII values and are in a human readable format For example, any file with a etc.
The text files can be of two types : Regular text files : created in notpad. These files have a file extension as .txt
Delimited Text files: A special character is stored to separate the values. when comma(,) used files called CSV files , when Tab space(\t) is used files called TSV (tab separated values)
Theory questions
.
Text Files: Sequence of lines and each line is a sequence of characters (ASCII) or UNICODE characters/Each line is terminated by a special character, known as End of Line (EOL). By default the EOL will be '\n' (New line character) / So at the lowest level, text file will be collection of bytes. Text files are stored in human readable format such as .txt, .c, ,py, .rtf et.c and they can also be created using any text editor.
A text file stores data in the form of alphabets, digits and other special symbols by storing their ASCII values and are in a human readable format For example, any file with a etc.
The text files can be of two types : Regular text files : created in notpad. These files have a file extension as .txt
Delimited Text files: A special character is stored to separate the values. when comma(,) used files called CSV files , when Tab space(\t) is used files called TSV (tab separated values)
Reading Writing text files:
.count words in a text file
f=open('abc.txt')
c=0
s=f.read()
w=s.split()
for i in w:
c=c+1
print(c)
f.close()
Count and print words having length <=3 from file
f=open('abc.txt')
c=0
s=f.read()
w=s.split()
for i in w:
if(len(i)<=3): #match the length of item in list
print(i)
c=c+1
print(c)
Count lines in a file
f=open('abc.txt')
c=0
list=f.readlines() # Read lines from file and store in a list named list
for i in list:
c=c+1
print(c)
f.close()
Define countmy function to count “my’ or “My” word
def countmy():
f=open('story.txt','r')
c=0
s=f.read()
w=s.split()
for i in w:
if(i=="my or My"):
c=c+1
print("my or My occurs",c,"times")
f.close()
countmy()
Count lines starts from ‘s’
f=open('abc.txt')
c=0
list=f.readlines()
for i in list:
if(i[0]=='s'):
c=c+1
print(c)
f.close()
Count number of digits present in text file
f=open('abc.txt')
count=0
s=f.read()
w=s.split()
for i in w:
for c in i:
if(i.isdigit()):
count=count+1
print(count)
Count lower case alphabets from file
if(i.islower()):
Count upper case alphabets from file
if(i.isupper()):
Count characters from file
f=open('abc.txt')
count=0
s=f.read()
w=s.split()
for i in w: # traverse every item in list
for c in i: # traverse every character in item
count=count+1
print(count)
Reading lines from one file and write into another file starting from 's'
r=open('read.txt',"r") w=open("write.txt",'w') lines=r.readlines()
for line in lines:
if(line[0]=='s'):
w.write(line)
r.close()
w.close()
Write only digits from one file to another
fr=open('read.txt',"r")
fw=open("write.txt",'w')
s=fr.read()
w=s.split()
for i in w:
for c in i:
if c.isdigit():
fw.write(c)
fr.close()
fw.close()
Name the functions used for reading text from a text file ?
read() and readlines()
Q1. Write a function to read a text file and display the count of vowels and consonants in the file
Q2. Write a function that print only first and last line of text file abc.txt
Q3. Write a function that print longest line from a text file abc.txt
Q4. Write a function count that Count lower case alphabets from text file xyz.txt
Q5.Write a function that Count number of digits present in text file hello.txt
Q6. Write a function countchar that count all Count characters from file demo.txt
Q7.Write a function that count lines starts from 's' or 'S' from text file abc.txt
Q8. Write a function that Count ‘india’ word from text file abc.txt
Q9. Write a function the Count and display all words present in a text file abc.txt
Q10.Write a user-defined function named count() that will read the contents of text file named “Story.txt” and count the number of lines which starts with either “I‟ or “M‟. E.g. In the following paragraph, there are 2 lines starting with “I‟ or “M‟:
“India is the fastest growing economy.
India is looking for more investments around the globe.
The whole world is looking at India as a great market.
Most of the Indians can foresee the heights that India is capable of reaching.”
Q11. Write a function in python to count the number of lowercase alphabets present in a text file “Story.txt”
Q12. 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.
Q13. 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.
Q14. Write a user defined function in Python that displays the number of lines starting with ‘H’ in the file story.txt. Eg: if the file contains:
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.
Then the line count should be 2
Q15. Write a user defined function countwords() in python to count how many words are present in a text file named “story.txt”. For example, if the file story.txt contains following text:
Co-education system is necessary for a balanced society. With co-education system, Girls and Boys may develop a feeling of mutual respect towards each other.
The function should display the following:
Total number of words present in the text file are: 24
Q16. Write a function countmy( )in Python to read the text file “Story.txt” and count the number of times “my” or “My” occurs in the file. For example if the file “Story.TXT” contains:
“This is my website. I have displayed my preferences in the CHOICE section.”
The countmy( ) function should display the output as: “my occurs 2 times”.
Click here for solutions