Reputation: 27
So I was supposed to find the number of occurences of a text file, by following this kind of format.
But my code probably is entirely wrong.
text = open('text3.txt','r')
d = dict()
fname = input('Enter the file name:')
l = input('Enter letter to be searched:')
k = 0
with open(fname, 'r') as f:
for line in f:
words = line.split()
for i in words:
for letter in i:
if(letter == 1):
k = k+1
print('Occurences of the letter:')
print(k)
Can someone help me for this?
Upvotes: 0
Views: 92
Reputation: 159
yeah you can iterate the characters instead:
fname = input('Enter the file name:')
char = input('Enter letter to be searched:')
i = 0
with open(fname, 'r') as f:
data = f.read()
for each in data:
if each == char:
i += 1
print(i)
Upvotes: 2