我的python代码将输出打印两次。
我无法正确打印输出。请帮助。预先感谢
fname = input("Enter file name: ")
if len(fname) < 1 :
fname = "mbox-short.txt"
fh = open(fname)
count = 0
for ln in fh:
if ln.startswith("From"):
count = count + 1
b2 = ln.split()
print(b2[1])
print("There were", count, "lines in the file with From as the first word")
答案 0 :(得分:0)
在for循环中使用print(b [0])
答案 1 :(得分:0)
我在pycharm上执行了您的代码,文件为 abc.txt ,内容为:
From dhfgdfjdfvdf dfd f
from bjddfgdhgfkdgfkd
dhdgfdhfgj
dfhdgfgdhfg
From hgfdfgdgsdguig
得到的结果是
Enter file name:
dhfgdfjdfvdf
hgfdfgdgsdguig
There were 2 lines in the file with From as the first word
因此根据代码,没有两个最终输出,而我假设
print(b2[1])
是故意的。