Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Generate a date time string that I can use in for MySQL

How to do this:

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)

this will end up in an error:

print time.strftime(r"%d.%m.%Y %H:%M:%S", time.localtime()) exceptions.AttributeError: 'builtin_function_or_method' object has no attribute 'strftime'

I found plenty of information - but it seems as if I either need to put it directly into an sql query or import anything else? I'd like to have the time in a string first to pass it to my object.

like image 946
Chris Avatar asked Jan 09 '23 09:01

Chris


1 Answers

You are importing a time() function from the time module. Instead, you need to import the module. Replace:

from time import time

with:

import time

Demo:

>>> 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'

As a side note, another option to have a time stamp attached to a scraped item in the database, would be to use NOW() MySQL function and call it while inserting an item into the database in the pipeline.

like image 85
alecxe Avatar answered Jan 14 '23 10:01

alecxe