django - Python: Formatting lists of lists -



django - Python: Formatting lists of lists -

i'm newbie python. have code below extracts 5 rows mysql table.

if keyword.lower() == 'last5': cursor.execute('select income_ref, income_amount model_income company_mobilenumber = %s order income_datetime desc limit 5', [sender]) result = cursor.fetchall() result = [list(row) row in result] self.respond(result) homecoming ()

the self.respond() kinda works print() sends text message instead.i having problem formatting output. code above list of lists looks likes this:

[[u'2014-11-06fd753b-inc', u'50.0'], [u'2014-11-067d724b-inc', u'50.0'], [u'2014-11-067557d6-inc', u'50.0']]

i wish new how create this:

ref:2014-11-06fd753b-inc amount:50.0 ref:2014-11-067d724b-inc amount:50.0 ref:2014-11-067557d6-inc amount:50.0

that includes prefixing 'ref' , 'amount' before each respective field. swear have searched high , low on how week i'm failing crack it.

you can use:

self.respond("\n".join("ref:{} amount:{}".format(ref, amt) ref, amt in result))

not beginner material, but...

joining string (created formatting "ref:{} amount:{}") "\n" using generator

if keyword.lower() == 'last5': cursor.execute('select income_ref, income_amount model_income company_mobilenumber = %s order income_datetime desc limit 5', [sender]) result = cursor.fetchall() result = [list(row) row in result] self.respond("\n".join("ref:{} amount:{}".format(ref, amt) ref, amt in result)) homecoming ()

i left list comprehension result = [list(row) row in result] because don't know fetchall() returns.

output:

ref:2014-11-06fd753b-inc amount:50.0 ref:2014-11-067d724b-inc amount:50.0 ref:2014-11-067557d6-inc amount:50.0

python django python-2.7

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -