在Python中使用time.time() - 错误似乎没有意义

时间:2016-12-06 00:11:04

标签: python variables time

使用time.time()作为轻松安排任务的方法,但由于某种原因,我不断收到一个对我来说没有多大意义的错误。我打印了 timenow = time.time() def my_BidAsk(msg): global timenow tnow = time.time() if (tnow - timenow) >= 60: resample() timenow = time.time() 并且读得正确 代码:

06-Dec-16 01:09:18 ERROR     Exception in message dispatch.  Handler 'my_BidAsk' for 'tickPrice'
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/ib/opt/dispatcher.py", line 44, in __call__
    results.append(listener(message))
  File "/Users/usr/Desktop/Hobbies/Coding/connect-contract-order IB.py", line 57, in my_BidAsk
    tnow = time.time()
UnboundLocalError: local variable 'time' referenced before assignment

这是我得到的错误,但它没有意义,因为无论我是否在全局或本地预定义它,错误仍然是相同的。知道为什么吗?

Arc

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,但我能够解决。就我而言,我稍后在同一个函数中初始化了一个名为“time”的局部变量。使用库名作为局部变量名会导致在脚本运行“t_start = time.time()”时引发异常。

为了解决这个问题,我将我的局部变量从“time”重命名为无害的东西(在我的例子中是“time_s”)。忽略任何不良的熊猫做法...

t_start = time.time()

for index, row in profile.iterrows():
    time = row['time_s']
    current = row['Current']
    voltage = row['Voltage_V']
    soc = row['SOC']

改为:

t_start = time.time()

for index, row in profile.iterrows():
    time_s = row['time_s']
    current = row['Current']
    voltage = row['Voltage_V']
    soc = row['SOC']