[英]Python -- output repeated
fname = input("Enter file name: ")
fh = open(fname)
count = 0
for line in fh:
line = line.rstrip()
if not line.startswith("From"):
continue
wds = line.split()
print(wds[1])
count +=1
print("There were", count, "lines in the file with From as the first word")
#輸出樣本#
stephen.marquard@uct.ac.za
stephen.marquard@uct.ac.za
louis@media.berkeley.edu
louis@media.berkeley.edu
zqian@umich.edu
zqian@umich.edu
我用變通方法修復了代碼,但只是想知道為什么它使 output 加倍。謝謝
據我所知,這不是代碼的問題。 作為測試,我制作了一個 txt 文檔並調用了該特定文檔。 它給出了正確的 output。
文本文檔是:來自@ABCDEFG 來自@checkA 來自@checkB
@checkC
來自@checkD
fname = "文本.txt"
fh = open(fname) 計數 = 0
for line in fh: line = line.rstrip() if not line.startswith("From"): continue wds = line.split() print(wds[1]) count +=1
print("There were", count, "文件中以 From 作為第一個單詞的行")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.