Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Duplicating values
#1
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
Reply
#2
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.
Reply
#3
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Duplicating Rows And Put Them In Certain Index Position Query eddywinch82 0 1,295 Nov-25-2020, 05:24 PM
Last Post: eddywinch82
  strange effect from duplicating a file descriptor Skaperen 1 2,079 Feb-18-2019, 08:20 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020