Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #37801

Re: Formatting a column's value output

Newsgroups comp.lang.python
Date 2013-01-28 01:44 -0800
References (7 earlier) <mailman.1116.1359311811.2939.python-list@python.org> <mailman.1118.1359312305.2939.python-list@python.org> <d88b7c4c-5576-4477-ae29-c9226ec0e1e4@googlegroups.com> <30d46f31-c056-45cf-8902-213536dc11fc@googlegroups.com> <mailman.1121.1359319842.2939.python-list@python.org>
Subject Re: Formatting a column's value output
From Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com>
Message-ID <mailman.1127.1359366844.2939.python-list@python.org> (permalink)

Show all headers | View raw


Τη Κυριακή, 27 Ιανουαρίου 2013 10:50:33 μ.μ. UTC+2, ο χρήστης Mitya Sirenef έγραψε:
> On 01/27/2013 03:24 PM, οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½ οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½ wrote:
> 
> > οΏ½οΏ½ οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½, 27 οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½ 2013  9:12:16 οΏ½.οΏ½. UTC+2, οΏ½ οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½οΏ½ ru...@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?
> 
> 
> 
> 
> 
> 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


Thank you very much, your solution makes the code looks so much clearer!

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 09:51 -0800
  Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 05:04 +1100
    Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 11:41 -0800
      Re: Formatting a column's value output Michael Torrie <torriem@gmail.com> - 2013-01-26 15:26 -0700
        Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-27 01:04 -0800
          Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 20:08 +1100
            Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-27 01:16 -0800
              Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 20:26 +1100
                Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 01:30 -0800
                Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 01:30 -0800
            Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-27 01:16 -0800
          Re: Formatting a column's value output rurpy@yahoo.com - 2013-01-27 08:12 -0800
            Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 10:05 -0800
              Re: Formatting a column's value output Joel Goldstick <joel.goldstick@gmail.com> - 2013-01-27 13:36 -0500
                Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 10:44 -0800
                Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 10:44 -0800
                Re: Formatting a column's value output rurpy@yahoo.com - 2013-01-27 11:12 -0800
                Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 12:24 -0800
                Re: Formatting a column's value output Mitya Sirenef <msirenef@lightbird.net> - 2013-01-27 15:50 -0500
                Re: Formatting a column's value output rurpy@yahoo.com - 2013-01-27 14:27 -0800
                Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-28 01:47 -0800
                Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-28 01:44 -0800
                Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-28 01:44 -0800
            Re: Formatting a column's value output Virgil Stokes <vs@it.uu.se> - 2013-01-27 18:46 +0100
        Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-27 01:04 -0800
    Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 11:41 -0800
    Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 13:07 -0800
      Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 09:18 +1100
        Re: Formatting a column's value output alex23 <wuwei23@gmail.com> - 2013-01-26 15:29 -0800
          Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 10:38 +1100
          Re: Formatting a column's value output Jason Friedman <jsf80238@gmail.com> - 2013-02-03 09:18 -0800
            Re: Formatting a column's value output Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-04 06:39 +0000
    Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 13:07 -0800

csiph-web