Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Formatting a column's value output

Reply
Thread Tools

Formatting a column's value output

 
 
Guest
Posts: n/a
 
      01-27-2013
, 27 2013 6:12:59 .. UTC+2, (E-Mail Removed) :
> On 01/27/2013 02:04 AM, Ferrous Cranus wrote:
>
> >[...]

>
> > data = cur.fetchall()

>
> > for row in data:

>
> > print ( "<tr>" )

>
> >

>
> > for item in row:

>
> > print( '''<td> <a href='http://www.%s?show=log'>%s</a> </td>''' % (item, item) )

>
> >[...]

>
> > Okey, so far BUT i want the url linking to happen only for the URL column's

>
> > value, and not for the hits column too. How do i apply the url link to the

>
> > URL column's value only?

>
>
>
> Ferrous,
>
>
>
> 'row' has two items (the url and the hit count) in it, right?
>
> So print each separately rather than in a loop:
>
>
>
> data = cur.fetchall()
>
> for row in data:
>
> url = row[0]
>
> hits = row[1]
>
> print ( "<tr>" )
>
> print( "<td> <a href='http://www.%s?show=log'>%s</a> </td>:% (url, hits) )


Yes that can work, but i want to ask you if its possible to do the same thing from within the loop as i have it coded it until now.

Is there a way to seperate the values within the loop?
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      01-27-2013
, 27 2013 8:36:42 .. UTC+2, Joel Goldstick :
> On Sun, Jan 27, 2013 at 1:05 PM, <(E-Mail Removed)> wrote:
>
> , 27 2013 6:12:59 .. UTC+2, (E-Mail Removed) :
>
>
>
>
> > On 01/27/2013 02:04 AM, Ferrous Cranus wrote:

>
> >

>
> > >[...]

>
> >

>
> > > * * * * * * data = cur.fetchall()

>
> >

>
> > > * * * * * * for row in data:

>
> >

>
> > > * * * * * * * * * * print ( "<tr>" )

>
> >

>
> > >

>
> >

>
> > > * * * * * * * * * * for item in row:

>
> >

>
> > > * * * * * * * * * * * * * * print( '''<td> *<a href='http://www.%s?show=log'>%s</a> *</td>''' % (item, item))

>
>
> >

>
> > >[...]

>
> >

>
> > > Okey, so far BUT i want the url linking to happen only for the URL column's

>
> >

>
> > > value, and not for the hits column too. How do i apply the url link to the

>
> >

>
> > > URL column's value only?

>
> >

>
> >

>
> >

>
> > Ferrous,

>
> >

>
> >

>
> >

>
> > 'row' has two items (the url and the hit count) in it, right?

>
> >

>
> > So print each separately rather than in a loop:

>
> >

>
> >

>
> >

>
> > * * data = cur.fetchall()

>
> >

>
> > * * for row in data:

>
> >

>
> > * * * url = row[0]

>
> >

>
> > * * * hits = row[1]

>
> >

>
> > * * * * print ( "<tr>" )

>
> >

>
> > * * * * print( "<td> *<a href='http://www.%s?show=log'>%s</a> *</td>: % (url, hits) )

>
>
>
> Yes that can work, but i want to ask you if its possible to do the same thing from within the loop as i have it coded it until now.
>
>
>
> Is there a way to seperate the values within the loop?
>
>
>
> for row in data:
>
> * * print( "<td> *<a href='http://www.%s?show=log'>%s</a> *</td>: " % (row[0], row[1]) )


This is not correct.
the <a href> attribute is for url (row[0]) only, not for url and hits too.

i want the following working code:

=============
data = cur.fetchall()
for row in data:
url = row[0]
hits = row[1]
print ( "<tr>" )
print( "<td> <a href='http://www.%s?show=log'>%s</a> </td>" % (url, url) )
print( "<td><b> %s </td>" % (hits) )
=============

inside the loop i posted initially, if its possible.
 
Reply With Quote
 
 
 
 
Guest
Posts: n/a
 
      01-27-2013
, 27 2013 8:36:42 .. UTC+2, Joel Goldstick :
> On Sun, Jan 27, 2013 at 1:05 PM, <(E-Mail Removed)> wrote:
>
> , 27 2013 6:12:59 .. UTC+2, (E-Mail Removed) :
>
>
>
>
> > On 01/27/2013 02:04 AM, Ferrous Cranus wrote:

>
> >

>
> > >[...]

>
> >

>
> > > * * * * * * data = cur.fetchall()

>
> >

>
> > > * * * * * * for row in data:

>
> >

>
> > > * * * * * * * * * * print ( "<tr>" )

>
> >

>
> > >

>
> >

>
> > > * * * * * * * * * * for item in row:

>
> >

>
> > > * * * * * * * * * * * * * * print( '''<td> *<a href='http://www.%s?show=log'>%s</a> *</td>''' % (item, item))

>
>
> >

>
> > >[...]

>
> >

>
> > > Okey, so far BUT i want the url linking to happen only for the URL column's

>
> >

>
> > > value, and not for the hits column too. How do i apply the url link to the

>
> >

>
> > > URL column's value only?

>
> >

>
> >

>
> >

>
> > Ferrous,

>
> >

>
> >

>
> >

>
> > 'row' has two items (the url and the hit count) in it, right?

>
> >

>
> > So print each separately rather than in a loop:

>
> >

>
> >

>
> >

>
> > * * data = cur.fetchall()

>
> >

>
> > * * for row in data:

>
> >

>
> > * * * url = row[0]

>
> >

>
> > * * * hits = row[1]

>
> >

>
> > * * * * print ( "<tr>" )

>
> >

>
> > * * * * print( "<td> *<a href='http://www.%s?show=log'>%s</a> *</td>: % (url, hits) )

>
>
>
> Yes that can work, but i want to ask you if its possible to do the same thing from within the loop as i have it coded it until now.
>
>
>
> Is there a way to seperate the values within the loop?
>
>
>
> for row in data:
>
> * * print( "<td> *<a href='http://www.%s?show=log'>%s</a> *</td>: " % (row[0], row[1]) )


This is not correct.
the <a href> attribute is for url (row[0]) only, not for url and hits too.

i want the following working code:

=============
data = cur.fetchall()
for row in data:
url = row[0]
hits = row[1]
print ( "<tr>" )
print( "<td> <a href='http://www.%s?show=log'>%s</a> </td>" % (url, url) )
print( "<td><b> %s </td>" % (hits) )
=============

inside the loop i posted initially, if its possible.
 
Reply With Quote
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      01-27-2013
On 01/27/2013 11:44 AM, Κώστας *απαδόπουλος wrote:
> This is not correct.
> the <a href> attribute is for url (row[0]) only, not for url and hits too..
>
> i want the following working code:
>
> =============
> data = cur.fetchall()
> for row in data:
> url = row[0]
> hits = row[1]
> print ( "<tr>" )
> print( "<td> <a href='http://www.%s?show=log'>%s</a> </td>" % (url, url) )
> print( "<td><b> %s </td>" % (hits) )
> =============
>
> inside the loop i posted initially, if its possible.


The easiest way is to separate them when you read 'row'
data = cur.fetchall()
for row in data:
for url,hits in row:
print ( "<tr>" )
print( "<td> <a href='http://www.%s?show=log'>%s</a> </td>: % (url, hits) )
Note that sql query you used guaranties that each row will contain
exactly two items so there is no point in using a loop to go through
each row item as you would if you did not know how many items it
contained.

But if you insist on going through them item by item in a loop, then
you need to figure out which item is which inside the loop. One way
is like:

data = cur.fetchall()
for row in data:
print ( "<tr>" )
item_position = 0
for item in row:
if item_position == 0:
print( "<td> <a href='http://www.%s?show=log'>%s</a> </td>:" % (item[0], item[0]))
if item_position == 1:
print( "<td><b> %s </td>" % item[1] )
item_position += 1

There are many variations of that. You can simplify the above a
little by getting rid of the "item_position=" lines and replacing
"for item in row:" with "for item_position, item = enumerate (item):"
But I think you can see the very first code example above is way
more simple.

By the way, it would be a lot easier to read your posts if you
would snip out the extra blank lines that Google Groups adds.
There are some suggestions in
http://wiki.python.org/moin/GoogleGroupsPython

 
Reply With Quote
 
Guest
Posts: n/a
 
      01-27-2013
, 27 2013 9:12:16 .. UTC+2, (E-Mail Removed) :
> <python code>


Yes indeed, there is no need to use a loop since i know the exact number ofitems 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?
 
Reply With Quote
 
Mitya Sirenef
Guest
Posts: n/a
 
      01-27-2013
On 01/27/2013 03:24 PM, wrote:
> , 27 2013 9:12:16 .. UTC+2, (E-Mail Removed) :
>> <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

 
Reply With Quote
 
rurpy@yahoo.com
Guest
Posts: n/a
 
      01-27-2013
On 01/27/2013 01:50 PM, Mitya Sirenef wrote:
> On 01/27/2013 03:24 PM, Κώστας *απαδόπουλος wrote:
>> Τη Κυριακή, 27 Ιανουαρίου 2013 9:12:16 μ.μ. UTC+2, ο χρήστης (E-Mail Removed) *γραψε:
> >> <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" )


Or alternatively,

for row in data:
print ("tr>")
for item_num, item in enumerate (row):
if item_num != len(row) - 1:
print( "blah blah blah" )
else:
print( item.strftime(...) )

Or, being a little clearer,

LASTVISIT_POS = 4
for row in data:
print ("tr>")
for item_num, item in enumerate (row):
if item_num != LASTVISIT_POS:
print( "blah blah blah" )
else:
print( item.strftime(...) )
 
Reply With Quote
 
Guest
Posts: n/a
 
      01-28-2013
, 27 2013 10:50:33 .. UTC+2, Mitya Sirenef :
> On 01/27/2013 03:24 PM, ������ ������������ wrote:
>
> > �� �������, 27 ���������� 2013 9:12:16 �.�. UTC+2,� ������� (E-Mail Removed) ������:

>
> >> <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!
 
Reply With Quote
 
Guest
Posts: n/a
 
      01-28-2013
, 27 2013 10:50:33 .. UTC+2, Mitya Sirenef :
> On 01/27/2013 03:24 PM, ������ ������������ wrote:
>
> > �� �������, 27 ���������� 2013 9:12:16 �.�. UTC+2,� ������� (E-Mail Removed) ������:

>
> >> <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!
 
Reply With Quote
 
Guest
Posts: n/a
 
      01-28-2013
, 28 2013 12:27:12 .. UTC+2, (E-Mail Removed) :
> On 01/27/2013 01:50 PM, Mitya Sirenef wrote:
>
> > On 01/27/2013 03:24 PM, wrote:

>
> >> , 27 2013 9:12:16 .. UTC+2, (E-Mail Removed) :

>
> > >> <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) ORDERBY

>
> > 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" )

>
>
>
> Or alternatively,
>
>
>
> for row in data:
>
> print ("tr>")
>
> for item_num, item in enumerate (row):
>
> if item_num != len(row) - 1:
>
> print( "blah blah blah" )
>
> else:
>
> print( item.strftime(...) )
>
>
>
> Or, being a little clearer,
>
>
>
> LASTVISIT_POS = 4
>
> for row in data:
>
> print ("tr>")
>
> for item_num, item in enumerate (row):
>
> if item_num != LASTVISIT_POS:
>
> print( "blah blah blah" )
>
> else:
>
> print( item.strftime(...) )


Thank you very much, your alternatives are great but i think i'll use Mity'as solution, its more easy to me. Thank you very much!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xslt: how to use value from xml data as an attribute value in output html? =?UTF-8?b?5byg6Z+h5q2m?= XML 3 10-23-2006 02:31 PM
Formatting output fron Helper Function in Template? Syntax? D. Shane Fowlkes ASP .Net 1 05-19-2005 06:40 PM
Formatting Repeater oontrol output =?Utf-8?B?UGl0Y2Fpcm5pYQ==?= ASP .Net 1 02-17-2005 04:04 PM
Easy way to output a default value if <xsl:value-of/> selection comes back empty? Michael Ahlers XML 1 07-12-2004 05:17 PM
Need formatting options menu for formatting hard drive Mark T. Computer Support 3 11-24-2003 11:50 PM



Advertisments