[英]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'. 变量transaction_date(从Pandas数据框检索)的值为'03 / 04/2015'。 However, when I compare it to the string '03/04/2015', they are not equal. 但是,当我将其与字符串“ 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
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'
. transaction_date
是'03/04/15'
。 You're comparing it to '03/04/2015'
. 您正在将其与'03/04/2015'
进行比较。 Note the 20
. 注意20
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.