如何做到这一点:
from time import time
import datetime
current_time = time.strftime(r"%d.%m.%Y %H:%M:%S", time.localtime())
l.add_value('time', current_time)
这将以错误结束:
print time.strftime(r"%d.%m.%Y %H:%M:%S", time.localtime()) exceptions.AttributeError: 'builtin_function_or_method' object has no attribute 'strftime'
我找到了很多信息 - 但似乎我需要将其直接放入 sql 查询或导入其他任何东西? 我希望有时间在字符串中先将它传递给我的对象。
最佳答案
您正在从 time
模块中导入一个 time()
函数。相反,您需要导入模块。替换:
from time import time
与:
import time
演示:
>>> from time import time
>>> current_time = time.strftime(r"%d.%m.%Y %H:%M:%S", time.localtime())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'builtin_function_or_method' object has no attribute 'strftime'
>>> import time
>>> current_time = time.strftime(r"%d.%m.%Y %H:%M:%S", time.localtime())
>>> current_time
'13.05.2015 15:26:45'
作为旁注,另一种将时间戳附加到数据库中已抓取项目的选项是使用 NOW()
。 MySQL 函数并在将项目插入管道中的数据库时调用它。
关于Python:生成可用于 MySQL 的日期时间字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30214693/