Wednesday, December 23, 2015

django cursor fetchall change datetime.datetime and decimal stamp

Hello,

I am trying to use cursor to execute a sql and convert the results to a dictionary

def dictfetchall(cursor):
"Return all rows from a cursor as a dict"
columns = [col[0] for col in cursor.description]
return [
dict(zip(columns, row))
for row in cursor.fetchall()

This returns a value:

[{'created': datetime.datetime(2015, 12, 23, 4, 3, 53, 89779, tzinfo=<UTC>), 'to_address': 'Destination1', 'from_address': 'Source1', 'X': Decimal('0.0'), 'Y': Decimal('0.0')}]  

I want it to be converted to a readable datetime format and take the decimal out. I want the output to be of the form

[{'created': 20150404T00:00:000000Z, 'to_address': 'Destination1', 'from_address': 'Source1', 'X': '0.0', 'Y': '0.0'}]

Appreciate any help on this.

- Shekar

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/32bac38a-d98b-4123-994b-43ae72dce3cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment