比较Python字符串

时间:2016-07-19 21:57:54

标签: python string equality

我有以下代码。变量transaction_date(从Pandas数据帧中检索)的值为'03 / 04/2015'。但是,当我将它与字符串'03 / 04/2015'进行比较时,它们并不相同。

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

上面的代码产生以下输出。

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'

我怀疑这是某种间距问题,但我不确定如何纠正它。还有什么我可以查看这些字符串是如何不同的吗?

1 个答案:

答案 0 :(得分:3)

transaction_date'03/04/15'。您将其与'03/04/2015'进行比较。请注意20