Formatting a column's value output

F

Ferrous Cranus

try:
cur.execute( '''SELECT URL, hits FROM counters ORDER BY hits DESC''' )
except MySQLdb.Error, e:
print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
else:
data = cur.fetchall()

for row in data:
print ( "<tr>" )

for item in row:
print ( "<td><b><font color=yellow> %s </td>" % item )

sys.exit(0)
=====================================

In the aboce code wheb 'URL' is to be typed out be print i need it to be formatted as a link, so the viewer can click on it.

Is this possible please?

Thank you.
 
C

Chris Angelico

print ( "<td><b><font color=yellow> %s </td>" % item )
In the aboce code wheb 'URL' is to be typed out be print i need it to be formatted as a link, so the viewer can click on it.

Is this possible please?

Easy, just make a tuple of item,item and have two %s markers:

print( "<td><b><font color=yellow><a href='%s'>%s</a></td>" % (item, item) )

But you need to concern yourself with escaping. In fact, you already
needed to, just to produce it as text - but it's now even more
important. I strongly suggest you look into that.

ChrisA
 
F

Ferrous Cranus

Τη Σάββατο, 26 ΙανουαÏίου 2013 8:04:12 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
Easy, just make a tuple of item,item and have two %s markers:



print( "<td><b><font color=yellow><a href='%s'>%s</a></td>" % (item, item) )



But you need to concern yourself with escaping. In fact, you already

needed to, just to produce it as text - but it's now even more

important. I strongly suggest you look into that.

I can use triple (''') quoting so i dont have to escape special characters.

But i didnt understand you suggestion about the tuple.

The dataset returns many lines and i need to transfor only the URL column.....
Sorry i did not understood.
 
F

Ferrous Cranus

Τη Σάββατο, 26 ΙανουαÏίου 2013 8:04:12 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
Easy, just make a tuple of item,item and have two %s markers:



print( "<td><b><font color=yellow><a href='%s'>%s</a></td>" % (item, item) )



But you need to concern yourself with escaping. In fact, you already

needed to, just to produce it as text - but it's now even more

important. I strongly suggest you look into that.

I can use triple (''') quoting so i dont have to escape special characters.

But i didnt understand you suggestion about the tuple.

The dataset returns many lines and i need to transfor only the URL column.....
Sorry i did not understood.
 
F

Ferrous Cranus

Τη Σάββατο, 26 ΙανουαÏίου 2013 8:04:12 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
Easy, just make a tuple of item,item and have two %s markers:



print( "<td><b><font color=yellow><a href='%s'>%s</a></td>" % (item, item) )

That code works, but it creates links for both the URL and hits columns!
Only the URL must be linked not the hits column!
 
F

Ferrous Cranus

Τη Σάββατο, 26 ΙανουαÏίου 2013 8:04:12 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
Easy, just make a tuple of item,item and have two %s markers:



print( "<td><b><font color=yellow><a href='%s'>%s</a></td>" % (item, item) )

That code works, but it creates links for both the URL and hits columns!
Only the URL must be linked not the hits column!
 
C

Chris Angelico

That code works, but it creates links for both the URL and hits columns!
Only the URL must be linked not the hits column!

1) Trim your quotes. I can't be bothered doing your trimming for you,
so I'm now under-quoting.

2) Quit using Google Groups, or manually fix its brain-dead double-spacing.

3) Look into Python's formatting options and see how to solve your own
problem. You'll probably want to use .format() rather than %.

4) Look into HTML entity escaping and do not publish anything to the
web until you understand why what you had before was dangerous.

ChrisA
 
M

Michael Torrie

I can use triple (''') quoting so i dont have to escape special characters.

Hmm. I think you missed what he was getting at. He's not talking about
Python escape sequences. He's talking about HTML ones. There is a
function in one of the standard library modules that does this. I think
it's called html_sanitize or something. Google will find this.
But i didnt understand you suggestion about the tuple.

What don't you understand about it? It's basic python string formatting
(well python 2.x string formatting).

http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

A tuple is one method for passing variables into the string formatter.
So if you need to display something twice, just put in two "%s" in the
format string, and pass it the same variable twice.
 
A

alex23

1) Trim your quotes. I can't be bothered doing your trimming for you,
so I'm now under-quoting.

2) Quit using Google Groups, or manually fix its brain-dead double-spacing.

3) Look into Python's formatting options and see how to solve your own
problem. You'll probably want to use .format() rather than %.

4) Look into HTML entity escaping and do not publish anything to the
web until you understand why what you had before was dangerous.

5) Please stop writing code for his _commercial web hosting service_
for free.

Notice that his next question is to ask you to modify your solution to
provide _exactly_ what he wants. He has no interest in learning Python.
 
C

Chris Angelico

5) Please stop writing code for his _commercial web hosting service_
for free.

You mean, stop asking us to write &c.? With that edit, yes, I agree.

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

ChrisA
 
F

Ferrous Cranus

Τη ΚυÏιακή, 27 ΙανουαÏίου 2013 12:26:44 Ï€.μ. UTC+2, ο χÏήστης Michael Torrie έγÏαψε:
A tuple is one method for passing variables into the string formatter.

So if you need to display something twice, just put in two "%s" in the

format string, and pass it the same variable twice.

Yes i know what a tuple is, iam just telling that the next code:
================================
try:
cur.execute( '''SELECT URL, hits FROM counters ORDER BY hits DESC''' )
except MySQLdb.Error, e:
print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
else:
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) )

sys.exit(0)
=================================
1. ruteruns a dataset
2. seperate each rows
3. itermate over the items of a row.

Okey, so far BUT i want the url linking to happen only for the URL column'svalue, and not for the hits column too. How do i apply the url link to theURL column's value only?
 
F

Ferrous Cranus

Τη ΚυÏιακή, 27 ΙανουαÏίου 2013 12:26:44 Ï€.μ. UTC+2, ο χÏήστης Michael Torrie έγÏαψε:
A tuple is one method for passing variables into the string formatter.

So if you need to display something twice, just put in two "%s" in the

format string, and pass it the same variable twice.

Yes i know what a tuple is, iam just telling that the next code:
================================
try:
cur.execute( '''SELECT URL, hits FROM counters ORDER BY hits DESC''' )
except MySQLdb.Error, e:
print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] )
else:
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) )

sys.exit(0)
=================================
1. ruteruns a dataset
2. seperate each rows
3. itermate over the items of a row.

Okey, so far BUT i want the url linking to happen only for the URL column'svalue, and not for the hits column too. How do i apply the url link to theURL column's value only?
 
C

Chris Angelico

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?

Step 1: Learn to read documentation.
Step 2: Learn to write code, rather than just ask someone else to do
it for free.

ChrisA
 
F

Ferrous Cranus

Τη ΚυÏιακή, 27 ΙανουαÏίου 2013 11:08:15 Ï€.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
Step 1: Learn to read documentation.

Step 2: Learn to write code, rather than just ask someone else to do

it for free.

I have tried code and i have showed to you, bu i cannot get it to work as iwant, thats why i ask. The solution you provided me its not correct.
 
F

Ferrous Cranus

Τη ΚυÏιακή, 27 ΙανουαÏίου 2013 11:08:15 Ï€.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
Step 1: Learn to read documentation.

Step 2: Learn to write code, rather than just ask someone else to do

it for free.

I have tried code and i have showed to you, bu i cannot get it to work as iwant, thats why i ask. The solution you provided me its not correct.
 
C

Chris Angelico

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 11:08:15 ð.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:

I have tried code and i have showed to you, bu i cannot get it to work asi want, thats why i ask. The solution you provided me its not correct.

You have been given a number of hints. Coding requires work, not just
complaining that someone else's code "is not correct". Either pay
someone to do the work, or do it yourself, but don't expect and demand
that volunteers do it for you.

http://www.catb.org/esr/faqs/smart-questions.html

ChrisA
 
Ê

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

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 11:26:38 ð.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
You have been given a number of hints. Coding requires work, not just

complaining that someone else's code "is not correct". Either pay

someone to do the work, or do it yourself, but don't expect and demand

that volunteers do it for you.

This is a free usenet newsgroup about python problems, so i expect free *voluntary* help.
If you dont want to help then dont.
Its not that i didnt try to write the code, i tried and cannot proceed fromthere.
 
Ê

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

Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 11:26:38 ð.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
You have been given a number of hints. Coding requires work, not just

complaining that someone else's code "is not correct". Either pay

someone to do the work, or do it yourself, but don't expect and demand

that volunteers do it for you.

This is a free usenet newsgroup about python problems, so i expect free *voluntary* help.
If you dont want to help then dont.
Its not that i didnt try to write the code, i tried and cannot proceed fromthere.
 
R

rurpy

[...]
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) )
 
V

Virgil Stokes

[...]
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) )
It is nice to see some constructive feedback to Ferrous from the python-list.
This will hopefully help to get Ferrous on the right track.
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top