Drey Mandani
Drey Mandani

Reputation: 27

File Handling, Dictionary and Maps

So I was supposed to find the number of occurences of a text file, by following this kind of format. enter image description hereenter image description here

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

Answers (1)

MVB76
MVB76

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

Related Questions