2016-07-19 22 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'と比較しています。 20に注意してください。