Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Formatting a column's value output (http://www.velocityreviews.com/forums/t956976-formatting-a-columns-value-output.html)

Ferrous Cranus 01-26-2013 05:51 PM

Formatting a column's value output
 
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.

Chris Angelico 01-26-2013 06:04 PM

Re: Formatting a column's value output
 
On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
> 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

Ferrous Cranus 01-26-2013 07:41 PM

Re: Formatting a column's value output
 
Τη Σάββατο, 26 Ιανουαρίου 2013 8:04:12 μ.μ. UTC+2, ο χρήστης Chris Angelico *γραψε:
> On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
>
> > 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.


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.

Ferrous Cranus 01-26-2013 07:41 PM

Re: Formatting a column's value output
 
Τη Σάββατο, 26 Ιανουαρίου 2013 8:04:12 μ.μ. UTC+2, ο χρήστης Chris Angelico *γραψε:
> On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
>
> > 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.


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.

Ferrous Cranus 01-26-2013 09:07 PM

Re: Formatting a column's value output
 
Τη Σάββατο, 26 Ιανουαρίου 2013 8:04:12 μ.μ. UTC+2, ο χρήστης Chris Angelico *γραψε:
> On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
>
> > 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) )


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

Ferrous Cranus 01-26-2013 09:07 PM

Re: Formatting a column's value output
 
Τη Σάββατο, 26 Ιανουαρίου 2013 8:04:12 μ.μ. UTC+2, ο χρήστης Chris Angelico *γραψε:
> On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
>
> > 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) )


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

Chris Angelico 01-26-2013 10:18 PM

Re: Formatting a column's value output
 
On Sun, Jan 27, 2013 at 8:07 AM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote:
> 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

Michael Torrie 01-26-2013 10:26 PM

Re: Formatting a column's value output
 
On 01/26/2013 12:41 PM, Ferrous Cranus wrote:
> 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/std...ing-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.

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



alex23 01-26-2013 11:29 PM

Re: Formatting a column's value output
 
On Jan 27, 8:18*am, Chris Angelico <ros...@gmail.com> wrote:
> 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.

Chris Angelico 01-26-2013 11:38 PM

Re: Formatting a column's value output
 
On Sun, Jan 27, 2013 at 10:29 AM, alex23 <wuwei23@gmail.com> wrote:
> 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


All times are GMT. The time now is 11:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.