简体   繁体   中英

Comparing Python Strings

I have the following code. The variable transaction_date (which is retrieved from a Pandas dataframe) has the value '03/04/2015'. However, when I compare it to the string '03/04/2015', they are not equal.

for index, rows in df_per_line.iterrows():
    validations = rows['NB_VALID']
    transaction_date = rows['DATE_TRANSACTION']

    try:
    map_date_validation['03/04/2015'][line_num] += validations
        print 'first succeeded!'
        print transaction_date
        print type(transaction_date)
        print type('03/04/15')
        if transaction_date == '03/04/2015':
            print 'YEAH!'
        else:
            print 'NAY!'
        map_date_validation[transaction_date][line_num] += validations
    except KeyError:
        print 'weird date found!'
        print transaction_date
        #continue

The code above yields the following output.

first succeeded!
03/04/15
<type 'str'>
<type 'str'>
NAY!
weird date found!
03/04/15
Traceback (most recent call last):
  File "rouen_ticket_parser.py", line 108, in <module>
    print map_date_validation[transaction_date]
KeyError: '03/04/15'

I suspect that this is some kind of spacing issue, but I am not sure how to rectify it. Is there anything else I can check to see how these strings are different?

The transaction_date is '03/04/15' . You're comparing it to '03/04/2015' . Note the 20 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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