【发布时间】:2019-11-12 08:43:09
【问题描述】:
摆脱这个简单错误的最简单方法是什么:
'float' 和 'str' 的实例之间不支持'>'。
有些文章看起来很难解决这个问题,但在我看来,应该有一个简单的解决方法。喜欢把currentTime==(str(currentTime)). 其他我尝试的东西放在底部。我的代码:
df=pd.read_csv(file_name, header=None)
last3Rows=df.iloc[-3:]
for i in range(3):
lastRow = df.iloc[i]
tradeTime=lastRow[4]
currentTime=datetime.datetime.now().timestamp()
print (currentTime)
print(type(currentTime))
print (tradeTime)
print(type(tradeTime))
if currentTime > tradeTime:
print("inner loop reached")
我尝试了什么:
currentTime = datetime.strptime('%H:%M:%S')
给:
AttributeError: 模块 'datetime' 没有属性 'strptime'
currentTime = strptime('%H:%M:%S')
给:
AttributeError: 模块 'datetime' 没有属性 'strptime'
currentTime=datetime.datetime.now().time()
给:
TypeError: '>' 在 'datetime.time' 和 'str' 的实例之间不支持
【问题讨论】:
-
tradeTime是str。您需要将其转换为float。 -
@flakes ...或者,更好的是,将tradeTime转换为日期时间(使用
pd.to_datetime())并直接与datetime.datetime.now()进行比较。 -
我想我看到了真正的问题。我需要去掉 '15:45:05' 中的冒号以变为 154505,以便将其切换为浮点数。