Formatting a column's value output

Discussion in 'Python' started by Ferrous Cranus, Jan 26, 2013.

  1. 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.
     
    Ferrous Cranus, Jan 26, 2013
    #1
    1. Advertising

  2. On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <> 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
     
    Chris Angelico, Jan 26, 2013
    #2
    1. Advertising

  3. Τη Σάββατο, 26 ΙανουαÏίου 2013 8:04:12 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
    > On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <> 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, Jan 26, 2013
    #3
  4. Τη Σάββατο, 26 ΙανουαÏίου 2013 8:04:12 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
    > On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <> 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, Jan 26, 2013
    #4
  5. Τη Σάββατο, 26 ΙανουαÏίου 2013 8:04:12 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
    > On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <> 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, Jan 26, 2013
    #5
  6. Τη Σάββατο, 26 ΙανουαÏίου 2013 8:04:12 μ.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
    > On Sun, Jan 27, 2013 at 4:51 AM, Ferrous Cranus <> 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, Jan 26, 2013
    #6
  7. On Sun, Jan 27, 2013 at 8:07 AM, Ferrous Cranus <> 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
     
    Chris Angelico, Jan 26, 2013
    #7
  8. 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/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.

    > The dataset returns many lines and i need to transfor only the URL column....
    > Sorry i did not understood.
     
    Michael Torrie, Jan 26, 2013
    #8
  9. Ferrous Cranus

    alex23 Guest

    On Jan 27, 8:18 am, Chris Angelico <> 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.
     
    alex23, Jan 26, 2013
    #9
  10. On Sun, Jan 27, 2013 at 10:29 AM, alex23 <> 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
     
    Chris Angelico, Jan 26, 2013
    #10
  11. Τη ΚυÏιακή, 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?
     
    Ferrous Cranus, Jan 27, 2013
    #11
  12. Τη ΚυÏιακή, 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?
     
    Ferrous Cranus, Jan 27, 2013
    #12
  13. On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus <> wrote:
    > 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
     
    Chris Angelico, Jan 27, 2013
    #13
  14. Τη ΚυÏιακή, 27 ΙανουαÏίου 2013 11:08:15 Ï€.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
    > On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus <> wrote:
    >
    > > 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 tothe 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.


    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.
     
    Ferrous Cranus, Jan 27, 2013
    #14
  15. Τη ΚυÏιακή, 27 ΙανουαÏίου 2013 11:08:15 Ï€.μ. UTC+2, ο χÏήστης Chris Angelico έγÏαψε:
    > On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus <> wrote:
    >
    > > 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 tothe 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.


    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.
     
    Ferrous Cranus, Jan 27, 2013
    #15
  16. On Sun, Jan 27, 2013 at 8:16 PM, Ferrous Cranus <> wrote:
    > Ôç ÊõñéáêÞ, 27 Éáíïõáñßïõ 2013 11:08:15 ð.ì. UTC+2, ï ÷ñÞóôçò Chris Angelico Ýãñáøå:
    >> On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus <> wrote:
    >>
    >> > 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.

    >
    > 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
     
    Chris Angelico, Jan 27, 2013
    #16
  17. Ôç ÊõñéáêÞ, 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.
     
    Êþóôáò Ðáðáäüðïõëïò, Jan 27, 2013
    #17
  18. Ôç ÊõñéáêÞ, 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.
     
    Êþóôáò Ðáðáäüðïõëïò, Jan 27, 2013
    #18
  19. Ferrous Cranus

    Guest

    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) )
     
    , Jan 27, 2013
    #19
  20. On 27-Jan-2013 17:12, wrote:
    > 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) )

    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.
     
    Virgil Stokes, Jan 27, 2013
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vishnu
    Replies:
    1
    Views:
    1,234
    Patrick.O.Ige
    Mar 25, 2006
  2. Greg Smith
    Replies:
    0
    Views:
    547
    Greg Smith
    Apr 12, 2007
  3. Esperanza

    Template column base on another column value

    Esperanza, Feb 18, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    5
    Views:
    189
    Elton Wang
    Feb 21, 2005
  4. Replies:
    2
    Views:
    252
    jyoti
    Aug 19, 2006
  5. Greg Smith
    Replies:
    0
    Views:
    167
    Greg Smith
    Apr 12, 2007
Loading...

Share This Page