简体   繁体   English

Python -- output 重复

[英]Python -- output repeated

File name is a text file.文件名是一个文本文件。 searching for an e-mail address after FROM在 FROM 之后搜索电子邮件地址

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")

#Output Sample# #输出样本#

stephen.marquard@uct.ac.za

stephen.marquard@uct.ac.za 

louis@media.berkeley.edu

louis@media.berkeley.edu

zqian@umich.edu

zqian@umich.edu

I fixed the code with a workaround but just wanted to know why it doubled the output. Thanks我用变通方法修复了代码,但只是想知道为什么它使 output 加倍。谢谢

From what I can tell, it's not an issue with the code.据我所知,这不是代码的问题。 As a test, I made a txt doc and called upon that specific doc.作为测试,我制作了一个 txt 文档并调用了该特定文档。 It gave the correct output.它给出了正确的 output。

The text doc was: From @ABCDEFG From @checkA From @checkB文本文档是:来自@ABCDEFG 来自@checkA 来自@checkB

@checkC @checkC

From @checkD来自@checkD

fname = "text.txt" fname = "文本.txt"

fh = open(fname) count = 0 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 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") print("There were", count, "文件中以 From 作为第一个单词的行")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM