Python Forum

Full Version: Duplicating values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I've somehow managed to duplicate my results for a short line of code used to extract the emails from a text document. I'm trying to figure out why that's happening and I'm having a bit of trouble coming up with a solution. I figured maybe it had to do with how I wrote the "For" loop. The correct output should be exactly half of what is actually showing. (the "line = line.split()" is for when I print out the emails which is also printing double the emails as well. I didn't include it in this post for privacy reasons.)
fname = input("Enter file name: ")

fh = open(fname)
count = 0

for line in fh:
    line = line.strip()
    if not line.startswith("From"):
        continue
    line = line.split()
    count = count + 1

print("There were", count, "lines in the file with From as the first word")
Output:
There were 54 lines in the file with From as the first word
How do you know there are not 54 lines in the file with From as the first word? I don't see how Python could miscalculate that number with your code.
(Jul-15-2023, 06:53 PM)Gribouillis Wrote: [ -> ]How do you know there are not 54 lines in the file with From as the first word? I don't see how Python could miscalculate that number with your code.

Great question I went and looked into the file and found that there is also lines that start with "From:" thanks for helping me out here I knew I couldn't have messed it up. I only need the ones with "From" specifically so I'm gonna have to figure out how to differentiate from the 2. Sometimes all you need is a little detective work I suppose lol.