0% found this document useful (0 votes)
32 views

Revision Text File - Answers

The document contains 29 questions about analyzing text files in Python. It includes questions about counting characters, words, lines; extracting words by start/end characters; and analyzing content of lines and files.

Uploaded by

pranav karthick
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)
32 views

Revision Text File - Answers

The document contains 29 questions about analyzing text files in Python. It includes questions about counting characters, words, lines; extracting words by start/end characters; and analyzing content of lines and files.

Uploaded by

pranav karthick
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/ 8

Text file questions:

File Name: Sample.txt


Function name: textFile()

1. Count number of ‘S’ or ‘T’ from a text file.


def textFile():
f = open('sample.txt', 'r')
content = f.read()
s = 0
t = 0
for i in content:
if i in 'Ss':
s += 1
elif i in 'Tt':
t += 1
print('S:', s)
print('T:', t)
f.close()
textFile()

2. Count number of Vowels, Consonants, Spaces, Digits and Special characters from a text
file.

def textFile():
f = open('sample.txt', 'r')
content = f.read()
vowel = 0
consonant = 0
space = 0
digit = 0
special = 0
for i in content:
if i.isalpha():
if i in 'aeiouAEIOU':
vowel += 1
else:
consonant += 1
elif i == ' ':
space += 1
elif i.isdigit():
digit += 1
else:
special += 1
print('Vowel:', vowel)
print('Consonant:', consonant)
print('Space:', space)
print('Digit:', digit)
print('Special:',special)
f.close()
textFile()
3. Print the words that begin vowel

def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[0] in 'aeiouAEIOU':
print(i)
f.close()
textFile()

4. Print the words that begin the H or R

def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[0] in 'HhRr':
print(i)
f.close()
textFile()

5. Print the words that begin and end with the same characters.

def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[0] == i[-1]:
print(i)
f.close()
textFile()

6. Print the words that begin and end with consonant.

def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[0].isalpha() and i[-1].isalpha():
if i[0] not in 'aeiouAEIOU' and i[-1] not in 'aeiouAEIOU':
print(i)
f.close()
textFile()
7. Print the words that end with a digit

def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i[-1].isdigit():
print(i)
f.close()
textFile()

8. Print the three lettered words from a file

def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if len(i) == 3:
print(i)
f.close()
textFile()

9. Print the word that has the letter e in it.

def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if 'e' in i or 'E' in i:
print(i)
f.close()
textFile()

10. Print the words that has more than 5 characters in it.

def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if len(i) > 5:
print(i)
f.close()
textFile()
11. Print the words that has more than 2 ‘t’ letters in it.
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
if i.count('t') > 2:
print(i)
f.close()
textFile()
12. Count the number of words present in the file.
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
print("No of words in the file:", len(words))
f.close()
textFile()
13. Count the words that begin the K or ‘k’
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
print("No of words in the file:", len(words))
f.close()
textFile()
14. Count the number of ‘this’ present in a file. (THIS, THis, tHIS, all possible combination to
be included)
def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
this = 0
for i in words:
if i.lower() == 'this':
this += 1
print("No of this words :", this)
f.close()
textFile()

15. Print the lines that begin with the letter T or W.


def textFile():
f = open('sample.txt', 'r')
for line in f:
if line[0] in 'TW':
print(line, end = '')
f.close()
textFile()
16. Print the lines that begin and end with same letter.
def textFile():
f = open('sample.txt', 'r')
for line in f:
if line[0] in 'TW':
print(line, end = '')
f.close()
textFile()
17. Print the lines that begin with the word ‘The’.(‘THE’, ‘THe’, ‘ThE’, all possible combination
to be included)
def textFile():
f = open('sample.txt', 'r')
for line in f:
w = line.split()
if w[0].lower() in 'the':
print(line, end = '')
f.close()
textFile()
18. Print the lines that has more than 10 words in it.
def textFile():
f = open('sample.txt', 'r')
for line in f:
w = line.split()
if len(w) > 10:
print(line, end = '')
f.close()
textFile()
19. Print the lines that has the characters ‘he’ in it. (‘he’ can be a separate word or can be a
part of word)
def textFile():
f = open('sample.txt', 'r')
for line in f:
if 'he' in line:
print(line, end = '')
f.close()
textFile()
20. Print the lines that has the word ‘from’ in it.(from, FROM, FroM, all possible combination
to be included)
def textFile():
f = open('sample.txt', 'r')
for line in f:
l = line.upper()
words = l.split()
if 'FROM' in words:
print(line, end = '')
f.close()
textFile()
21. Count the number of lines present in a file.

def textFile():
f = open('sample.txt', 'r')
lines = f.readlines()
print("No of lines in the file:", len(lines))
f.close()
textFile()

22. Count the number of characters present in each line of the file.
Output:
Line 1: 65
Line 2: 42
Line 3: 55

def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
print("Line",lineNumber,':',len(line))
lineNumber +=1
f.close()
textFile()

23. Count the number of words present in each line of the file.
Output:
Line 1: 12
Line 2: 8
Line 3: 11

def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
words = line.split()
print("Line",lineNumber,':',len(words))
lineNumber +=1
f.close()
textFile()

24. Count the number of ‘e’ present in each line.

def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
print("Line",lineNumber,':',line.count('e'))
lineNumber +=1
f.close()
textFile()
25. Count the number of ‘the’ words present in each line.

def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
l = line.upper()
words = l.split()
print("Line",lineNumber,':',words.count('THE'))
lineNumber +=1
f.close()
textFile()

26. Print the words that doesn’t have any vowels in it.

def textFile():
f = open('sample.txt', 'r')
content = f.read()
words = content.split()
for i in words:
w = i.upper()
if not('A' in w or 'E' in w or 'I' in w or 'O' in w or 'U' in w):
print(i)
textFile()

27. Print the lines that has more than 2 ‘is’ word in it.

def textFile():
f = open('sample.txt', 'r')
lineNumber = 1
for line in f:
l = line.upper()
words = l.split()
if words.count('IS') > 2:
print(line)

f.close()
textFile()

28. Print the lines that has more than 3 four lettered words.
def textFile():
f = open('sample.txt', 'r')
for line in f:
words = line.split()
c = 0
for w in words:
if len(w) == 4:
c += 1
if c > 3:
print(line)
f.close()
textFile()
29. Copy the lines that has the word ‘that’ in it to a new file named “thatword.txt’

def textFile():
f1 = open('sample.txt', 'r')
f2 = open('thatword.txt','a')
for line in f1:
l = line.upper()
words = l.upper()
if 'THAT' in words:
f2.write(line)
f1.close()
f2.close()

textFile()

You might also like