Python:生成可用于 MySQL 的日期时间字符串

标签 python mysql time scrapy

如何做到这一点:

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/

相关文章:

python - 使用 QTextCharFormat 更改选择颜色

c# - 在我的C#客户端中接收意外的udp数据包

mysql - linux 中用于处理数据库(oracle、mysql)的最佳 Gui 编辑器?

javascript - 计算剩余下载时间

c - 如何测量 Linux 上 C 语言的执行时间

python - Python 字典错误 : str object has no attribute append

python - 如何使用 LiveAPI(Ableton Live)发送剪辑名称

MYSQL从不同行选择2个具有相同值的不同字段

mysql - 在mysql中使用循环从另一个表插入数据

python-3.x - 给定两个音频文件,其中一个是另一个的摘要,是否有一种python方法来查找何时在原始文件中重播该摘要?