Python Forum
What's wrong with this if line?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What's wrong with this if line?
#1
This gives me the desired output:
targetfile = input(' Enter a file name: ')
#senders = [] --I don't think I need to create a list, do I?
sendercount = 0 #this counts number of senders
fhand = open(targetfile)
for line in fhand:
    words = line.split()
    if len(line) < 3:  continue #guardian to skip blank or short lines
    if words[0] == "From" and words[0]!= "From:":
            #senders.append(words[1])
        sendercount = sendercount + 1
        print(words[1])
    else: continue
print('There were',sendercount,'lines in the file with From as the first word.')
Not sure I really had it, I changed 8 to:
if words[0] != "From" and words[0] == "From:":
I got the exact same output: same e-mail addresses (second word in the format) and same number of lines (sendercount). With the first attempt I aimed to exclude the colon. The second attempt aims to include the colon.

What am I not understanding here?
Reply
#2
That is odd. Can you show us 5-10 of the lines that are triggering as senders?

Also, I would note that if words[0] == 'From' is True, it is automatically not True that words[0] == 'From:', and the other way around. So you don't need the !=, you just need the ==.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Sep-04-2019, 06:19 PM)ichabod801 Wrote: That is odd. Can you show us 5-10 of the lines that are triggering as senders?

Also, I would note that if words[0] == 'From' is True, it is automatically not True that words[0] == 'From:', and the other way around. So you don't need the !=, you just need the ==.

Ha. I'm glad you had me look at the file. It just so happens that this file is constructed with From Sender1.... From: Sender1.... From Sender2.... From: Sender2....

The From Sender1 lines have additional characters (date and time stamp) whereas the From: Sender1 lines have just two words.

So my initial attempt was probably correct. The two attempts would be targeting the different sets of lines, though.

Thanks for the help!
Reply
#4
I don't get why you need the second condition. If words[0] string is 'From' it for sure would be != 'From:', i.e. you don't do words[0].startswith('From') in which case it will make sense to add second condition to exclude 'From:'
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Well, if there's a line with and a line without for each email, then it stands to reason you would have the same number of lines. And since you are only printing the second word, it would stand to reason that you get the same emails.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Forum Jump:

User Panel Messages

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