Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
301 views
in Technique[技术] by (71.8m points)

Comparing two strings in Python not showing the intended result

This is an abstraction of a more complex script. The goal is to get information from unread emails and add it to a CSV file. It also needs to check whether the file exists in the first place. To simplify I put print statements instead of function calls. This still brings up the issue.

import ezgmail
import csv

orderEmails = ezgmail.search('subject:Automatic message, label:UNREAD')
unreadThreads = ezgmail.unread()
amountMessages = len(orderEmails[0].messages)
number: int
for number in range(amountMessages):
    message = orderEmails[0].messages[number]
    date = message.timestamp
    try:
        with open('file.csv','r') as file:
            csvReader = csv.reader(file, delimiter=',')
                for row in csvReader:
                    if row[0] == date:
                        print("Timestamp already exists")
                    else:
                        print("Ok")
    except FileNotFoundError:
        print("No such file")

Contents of file.csv

Date,Name,City
2020-12-27 18:11:16,John,New York
2020-12-29 17:44:23,Mary,Berlin

The variable "date" should be this string in the first run of the loop:

2020-12-27 18:11:16

Then this one:

2020-12-30 12:51:32

What I expected to get is:

Timestamp already exists
Ok

What I get instead is:

Ok
Ok
Ok
Ok
Ok
Ok

Besides obviously not understanding why this happens, I also don't understand why 6 "Ok" messages.

question from:https://stackoverflow.com/questions/65849116/comparing-two-strings-in-python-not-showing-the-intended-result

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're iterating over csvReader, which yields you the header row, too.

# First row
['Date', 'Name', 'City']

# Second row
['2020-12-27 18:11:16', 'John', 'New York']

# Third row
['2020-12-29 17:44:23', 'Mary', 'Berlin']

String 'Date' doesn't equal to string '2020-12-27 18:11:16', so the print("Ok") branch of your IF is executed.


As to why 6 "Ok" messages are being printed - I guess you're doing all that multiple times - because of the number stored in variable amountMessages.

for number in range(amountMessages):
   ...

Bonus LPT (Life Pro Tip) ??

If unsure, often the easiest thing to do is to put interactive debugger in there - and you can inspect variables at that point during execution.

If you have Python <3.7, you can simply put import pdb; pdb.set_trace() inside your code, like this:

...
    for row in csvReader:
        import pdb; pdb.set_trace()
        if row[0] == date
        ...
...

... and run your code. This way you will get interactive debugging session at that point, where you can do things like...

(Pdb) yay = 123
(Pdb) yay
123
(Pdb) row[0]
'Date'

If you have Python >=3.7, the built-in breakpoint() function is available, which you can use instead.

More info about pdb at Python official docs: https://docs.python.org/3/library/pdb.html


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...