Struggling to convert a mysql datetime object to a python string of a different format

Tim Chase python.list at tim.thechases.com
Sat Aug 7 19:12:04 CEST 2010


On 08/07/10 01:45, Νικόλαος Κούρας wrote:
>>    # variant B
>>    for row in dataset:
>>      host, hits, dt = row
>>      # rest of your code here
>
> So, row is a tuple comprising of 3 fields, and host, hist, dt
> are variables assigned each one of row's tuple values by
> breaking it to it's elements.
>
> But what kind of objects is host, hits, dt that containes the
> row's tuple data themselves? tuples or lists and why?

They contain the data of each respective element.  E.g.:

 >>> dataset = [
... (1, 'a', True),
... (2, 'b', False),
... ]
 >>>
 >>> for one, two, three in dataset:
...     print 'one%s = %r' % (type(one), one)
...     print 'two%s = %r' % (type(two), two)
...     print 'three%s = %r' % (type(three), three)
...     print '-' * 10
...
one<type 'int'> = 1
two<type 'str'> = 'a'
three<type 'bool'> = True
----------
one<type 'int'> = 2
two<type 'str'> = 'b'
three<type 'bool'> = False
----------

So likely in your case, "host" is a string, "hits" is an int, and 
"dt" is a datetime.datetime object.  The three of them together 
are the row as represented as a tuple:

   >>> type( (host, hits, dt) )
  <type 'tuple'>

which you can see in your own code by changing it temporarily to:

   for row in dataset:
     print type(row), len(row)

>>    # variant C
>>    for host, hits, dt in row:
>>      # rest of your code here
>>
> host, hits, data each and every one of them hold a piece of the row's
> tuple values.
>
> But what happens in here?

The same as Variant B, only it doesn't use the intermediate tuple 
"row".

> 'for host, hits, dt in dataset:'
>
> Here we don't have the row tuple. So what tthose variabels store, and in
> what datatype they strore info in and what is the difference between this
> and
>
>    'for host, hits, dt in row:'

The second one will fail because it would be the same as

   for tpl in row:
     host, hits, dt = tpl

The 1st time through the loop, tpl=host; the 2nd time through the 
loop, tpl=hits; and the 3rd time through the loop, tpl=dt

Attempting to do a tuple assignment (that 2nd line) will attempt 
to do something like

   host, hits, dt = "example.com"  # 1st pass through the loop
   host, hits, dt = 42  # 2nd pass through the loop
   host, hits, dt = datetime(2010,7,5)# 3rd pass through the loop

In most cases, it will fail on the first pass through the loop 
(except in the freak case your string value happens to have 3 
characters:

   >>> host, hits, dt = "abc" #exactly 3 letters
   >>> host
   'a'
)

> If the fieds datatypes returned form the database are for exmaple page
> varchar(50) , hits inteeger[11], date datetime then
> the when python grabs those results fields it would translate them to
> 'page as string' , (hits as int) , 'date as string' respectively?
> Whcih emans it translated those fileds returned to the most
> appropriate-most close to prototype stored in database' datatypes
> automatically?

Yes, except the internals (of the DB module...in this case mysql) 
are smart enough to translate the date into a datetime.datetime 
object, instead of a string.

>>     row = (host, hits, dt
>
> Would that be a row or a tuple when joined?

A "row" is a conceptual thing -- one row of data from your query. 
  It can be represented as either a tuple or a list (or any 
iteratable that represents "things in this row").  In this case 
(and I believe each row returned by a cursor.fetch*() call), it 
was tuple.

I hope that helps...it would behoove you to experiment with 
tuple-assignments such as the example code above so that you 
understand what it's doing in each case.

-tkc







More information about the Python-list mailing list