所以我应该按照这种格式来查找文本文件的出现次数。但我的代码可能是完全错误的。text = open('text3.txt','r')d = dict()fname = input('Enter the file name:')l = input('Enter letter to be searched:')k = 0with 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+1print('Occurences of the letter:')print(k)有人可以帮我吗?
1 回答
MYYA
TA贡献1868条经验 获得超4个赞
是的,您可以改为迭代字符:
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)
添加回答
举报
0/150
提交
取消