Python|在文本文件中查找“n”个字符的单词
本文旨在查找具有一定数量字符的单词。在下面提到的代码中,给出了一个Python程序来查找文本文件中包含三个字符的单词。
例子:
Input : Hello, how are you ?
Output :
how
are
you
代码: Python程序在文本文件中查找包含三个字符的单词
count = 1
chrw = ""
# text file
file = open('textfile.txt', 'r')
while 1:
sp = file.read(1)
if count<= 3:
chrw = chrw + sp
if count>3:
if sp ==" ":
count = 0
if len(chrw)>0:
print(chrw)
chrw =""
elif sp !=" ":
chrw =""
count = count + 1
if not sp:
break
file.close()
输出:
how
are
you