Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'error:': 0.05; 'data:': 0.07; 'host,': 0.07; 'try:': 0.07; 'indeed,': 0.09; 'python:': 0.09; 'charset:iso-8859-7': 0.15; 'received:74.55.86': 0.16; 'received:74.55.86.74': 0.16; 'received:smtp.webfaction.com': 0.16; 'received:webfaction.com': 0.16; 'row': 0.16; 'subject:Formatting': 0.16; 'syntax.': 0.16; 'sys.exit(0)': 0.16; 'wrote:': 0.17; 'specify': 0.17; 'written': 0.20; 'simpler': 0.22; 'header:In-Reply-To:1': 0.25; 'header:User- Agent:1': 0.26; 'existence': 0.29; 'code': 0.31; 'print': 0.32; 'anywhere': 0.33; 'skip:s 30': 0.33; 'to:addr:python-list': 0.33; 'thanks': 0.34; 'pm,': 0.35; 'there': 0.35; 'except': 0.36; 'but': 0.36; 'item': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'easier': 0.38; 'skip:l 20': 0.38; 'to:addr:python.org': 0.39; 'received:192': 0.39; 'where': 0.40; 'received:192.168': 0.40; 'email addr:yahoo.com': 0.61; 'more': 0.63; 'skip:= 30': 0.64; 'total': 0.65; 'as:': 0.75; 'you:': 0.75; 'absolutely': 0.84; '2013': 0.84; 'subject:value': 0.84; 'write:': 0.91 Date: Sun, 27 Jan 2013 15:50:33 -0500 From: Mitya Sirenef User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130106 Thunderbird/17.0.2 MIME-Version: 1.0 To: python-list@python.org Subject: Re: Formatting a column's value output References: <6ad78df4-0ec0-4760-8f5a-5ecf129c5e65@googlegroups.com> <63edba3c-55af-4c4c-a311-4188891bfdf0@googlegroups.com> <30d46f31-c056-45cf-8902-213536dc11fc@googlegroups.com> In-Reply-To: <30d46f31-c056-45cf-8902-213536dc11fc@googlegroups.com> Content-Type: text/plain; charset=ISO-8859-7; format=flowed Content-Transfer-Encoding: 8bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1359319842 news.xs4all.nl 6925 [2001:888:2000:d::a6]:58570 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37789 On 01/27/2013 03:24 PM, Κώστας Παπαδόπουλος wrote: > Τη Κυριακή, 27 Ιανουαρίου 2013 9:12:16 μ.μ. UTC+2, ο χρήστης ru...@yahoo.com έγραψε: >> > > 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 ( "" ) > > for item in host, useros, browser, hits, lastvisit.strftime('%A %e %b, %H:%M').decode('cp1253').encode('utf8'): > print ( "
%s " % 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? You can write: for row in data: print ("tr>") row = list(row) row[-1] = row[-1].strftime(...) for item in row: print( "blah blah blah" ) - mitya -- Lark's Tongue Guide to Python: http://lightbird.net/larks/ The existence of any evil anywhere at any time absolutely ruins a total optimism. George Santayana