2016-07-19 205 views
-1

我有以下代碼。變量transaction_date(從Pandas數據框中檢索)的值爲'03/04/2015'。但是,當我將它與字符串「03/04/2015」進行比較時,它們並不相同。比較Python字符串

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' 

我懷疑這是某種間距問題,但我不知道如何糾正它。還有什麼我可以檢查,看看這些字符串是如何不同?

回答

3

transaction_date'03/04/15'。你在比較'03/04/2015'。請注意0​​。