Formatting a column's value output

Ê

Êþóôáò Ðáðáäüðïõëïò

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 6:12:59 ì.ì. UTC+2, ï ÷ñÞóôçò (e-mail address removed) Ýãñáøå:
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?
 
Ê

Êþóôáò Ðáðáäüðïõëïò

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 8:36:42 ì.ì. UTC+2, ï ÷ñÞóôçò Joel Goldstick Ýãñáøå:
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?

'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.
 
Ê

Êþóôáò Ðáðáäüðïõëïò

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 8:36:42 ì.ì. UTC+2, ï ÷ñÞóôçò Joel Goldstick Ýãñáøå:
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?

'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.
 
R

rurpy

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
 
Ê

Êþóôáò Ðáðáäüðïõëïò

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 9:12:16 ì.ì. UTC+2, ï ÷ñÞóôçò (e-mail address 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?
 
M

Mitya Sirenef

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 9:12:16 ì.ì. UTC+2, ï ÷ñÞóôçò (e-mail address removed) Ýãñáøå:

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
 
R

rurpy

Τη ΚυÏιακή, 27 ΙανουαÏίου 2013 9:12:16 μ.μ. UTC+2, ο χÏήστης (e-mail address removed) έγÏαψε:

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(...) )
 
Ê

Êþóôáò Ðáðáäüðïõëïò

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 10:50:33 ì.ì. UTC+2, ï ÷ñÞóôçò Mitya Sirenef Ýãñáøå:
�� �������, 27 ���������� 2013 9:12:16 �.�. UTC+2,� ������� (e-mail address removed) ������:

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:



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

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 )




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!
 
Ê

Êþóôáò Ðáðáäüðïõëïò

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 10:50:33 ì.ì. UTC+2, ï ÷ñÞóôçò Mitya Sirenef Ýãñáøå:
�� �������, 27 ���������� 2013 9:12:16 �.�. UTC+2,� ������� (e-mail address removed) ������:

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:



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

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 )




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!
 
Ê

Êþóôáò Ðáðáäüðïõëïò

Ôç ÄåõôÝñá, 28 Éáíïõáñßïõ 2013 12:27:12 ð.ì. UTC+2, ï ÷ñÞóôçò (e-mail address removed) Ýãñáøå:
On 01/27/2013 03:24 PM, Êþóôáò Ðáðáäüðïõëïò wrote:
number of items i'am expecting. Thanks you very much for clarifying this
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'):
that 'lastvisit' column's value than needs formating,hence it makes me
use the above syntax.
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!
 
J

Jason Friedman

One of the difficulties on this list is that we don't have
two-dimensional people. Even our worst trolls have some redeeming
features. I can't just dismiss Ferrous out of hand...

Indeed, and that is a "problem" with humanity in general.
It is proof that God (or the universe) has a sense of humor.
You and the others on this list who answer questions are earning a
_lot_ of good karma.
 
S

Steven D'Aprano

Indeed, and that is a "problem" with humanity in general. It is proof
that God (or the universe) has a sense of humor.

If so, it's a vicious, nasty sense of humour. Imagine how simpler the
world would be if there were no shades of grey, and we could easily,
objectively and accurately divide people into two groups with no overlap:

- useful, nice, friendly people

- trolls and other low-lives


But no, even the worst scum have some redeeming features, and so we're
left with difficult subjective judgements, and nobody can agree whether
or not Person X should be pushed into a wood-chipper.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top