Formatting a column's value output

Κώστας Παπαδόπουλος nikos.gr33k at gmail.com
Sun Jan 27 15:24:50 EST 2013


Τη Κυριακή, 27 Ιανουαρίου 2013 9:12:16 μ.μ. UTC+2, ο χρήστης ru... at yahoo.com έγραψε:
> <python code>

Yes indeed, there is no need to use a loop since i know the exact number of items i'am expecting. Thanks you very much for clarifying this to me:
One last thing i want to ask you:

========================================
	try:
		cur.execute( '''SELECT host, userOS, browser, hits, lastvisit FROM visitors
						WHERE counterID = (SELECT ID FROM counters WHERE URL = %s) ORDER BY lastvisit DESC''', (htmlpage,) )
	except MySQLdb.Error, e:
		print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
	else:			
		data = cur.fetchall()
			
		for host, useros, browser, hits, lastvisit in data:
			print ( "<tr>" )

			for item in host, useros, browser, hits, lastvisit.strftime('%A %e %b, %H:%M').decode('cp1253').encode('utf8'):
				print ( "<td><center><b><font color=white> %s </td>" % item )
			
		sys.exit(0)
=======================================

That would be also written as:

for row in data:
    print ("tr>")
    for item in row:
        print( "blah blah blah" )

And that would make the code easier to read and more clear, but its that 'lastvisit' column's value than needs formating,hence it makes me use the above syntax.

Is there any simpler way to write the above working code without the need to specify all of the columns' names into the loop?



More information about the Python-list mailing list