Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #37728 > unrolled thread
| Started by | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| First post | 2013-01-26 09:51 -0800 |
| Last post | 2013-01-26 13:07 -0800 |
| Articles | 20 on this page of 33 — 11 participants |
Back to article view | Back to comp.lang.python
Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 09:51 -0800
Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 05:04 +1100
Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 11:41 -0800
Re: Formatting a column's value output Michael Torrie <torriem@gmail.com> - 2013-01-26 15:26 -0700
Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-27 01:04 -0800
Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 20:08 +1100
Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-27 01:16 -0800
Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 20:26 +1100
Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 01:30 -0800
Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 01:30 -0800
Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-27 01:16 -0800
Re: Formatting a column's value output rurpy@yahoo.com - 2013-01-27 08:12 -0800
Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 10:05 -0800
Re: Formatting a column's value output Joel Goldstick <joel.goldstick@gmail.com> - 2013-01-27 13:36 -0500
Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 10:44 -0800
Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 10:44 -0800
Re: Formatting a column's value output rurpy@yahoo.com - 2013-01-27 11:12 -0800
Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-27 12:24 -0800
Re: Formatting a column's value output Mitya Sirenef <msirenef@lightbird.net> - 2013-01-27 15:50 -0500
Re: Formatting a column's value output rurpy@yahoo.com - 2013-01-27 14:27 -0800
Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-28 01:47 -0800
Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-28 01:44 -0800
Re: Formatting a column's value output Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> - 2013-01-28 01:44 -0800
Re: Formatting a column's value output Virgil Stokes <vs@it.uu.se> - 2013-01-27 18:46 +0100
Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-27 01:04 -0800
Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 11:41 -0800
Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 13:07 -0800
Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 09:18 +1100
Re: Formatting a column's value output alex23 <wuwei23@gmail.com> - 2013-01-26 15:29 -0800
Re: Formatting a column's value output Chris Angelico <rosuav@gmail.com> - 2013-01-27 10:38 +1100
Re: Formatting a column's value output Jason Friedman <jsf80238@gmail.com> - 2013-02-03 09:18 -0800
Re: Formatting a column's value output Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-02-04 06:39 +0000
Re: Formatting a column's value output Ferrous Cranus <nikos.gr33k@gmail.com> - 2013-01-26 13:07 -0800
Page 1 of 2 [1] 2 Next page →
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-26 09:51 -0800 |
| Subject | Formatting a column's value output |
| Message-ID | <6ad78df4-0ec0-4760-8f5a-5ecf129c5e65@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-27 05:04 +1100 |
| Message-ID | <mailman.1086.1359223462.2939.python-list@python.org> |
| In reply to | #37728 |
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
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-26 11:41 -0800 |
| Message-ID | <d543dccf-af7a-4efd-adc5-13d9ef080c1e@googlegroups.com> |
| In reply to | #37729 |
Τη Σάββατο, 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.
[toc] | [prev] | [next] | [standalone]
| From | Michael Torrie <torriem@gmail.com> |
|---|---|
| Date | 2013-01-26 15:26 -0700 |
| Message-ID | <mailman.1090.1359239220.2939.python-list@python.org> |
| In reply to | #37732 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-27 01:04 -0800 |
| Message-ID | <a8690843-35b0-423e-a79d-f2b6c1991be6@googlegroups.com> |
| In reply to | #37741 |
Τη Κυριακή, 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's value, and not for the hits column too. How do i apply the url link to the URL column's value only?
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-27 20:08 +1100 |
| Message-ID | <mailman.1108.1359277704.2939.python-list@python.org> |
| In reply to | #37762 |
On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus <nikos.gr33k@gmail.com> 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
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-27 01:16 -0800 |
| Message-ID | <0f1f99da-f2fd-472c-8be3-d333289f7a9f@googlegroups.com> |
| In reply to | #37764 |
Τη Κυριακή, 27 Ιανουαρίου 2013 11:08:15 π.μ. UTC+2, ο χρήστης Chris Angelico έγραψε: > On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus <nikos.gr33k@gmail.com> 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 as i want, thats why i ask. The solution you provided me its not correct.
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-01-27 20:26 +1100 |
| Message-ID | <mailman.1109.1359278802.2939.python-list@python.org> |
| In reply to | #37765 |
On Sun, Jan 27, 2013 at 8:16 PM, Ferrous Cranus <nikos.gr33k@gmail.com> wrote: > Τη Κυριακή, 27 Ιανουαρίου 2013 11:08:15 π.μ. UTC+2, ο χρήστης Chris Angelico έγραψε: >> On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus <nikos.gr33k@gmail.com> 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 as i 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
[toc] | [prev] | [next] | [standalone]
| From | Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-27 01:30 -0800 |
| Message-ID | <83b86472-b769-42f1-b913-f69db7a23490@googlegroups.com> |
| In reply to | #37766 |
Τη Κυριακή, 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 from there.
[toc] | [prev] | [next] | [standalone]
| From | Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-27 01:30 -0800 |
| Message-ID | <mailman.1111.1359279020.2939.python-list@python.org> |
| In reply to | #37766 |
Τη Κυριακή, 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 from there.
[toc] | [prev] | [next] | [standalone]
| From | Ferrous Cranus <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-27 01:16 -0800 |
| Message-ID | <mailman.1110.1359278809.2939.python-list@python.org> |
| In reply to | #37764 |
Τη Κυριακή, 27 Ιανουαρίου 2013 11:08:15 π.μ. UTC+2, ο χρήστης Chris Angelico έγραψε: > On Sun, Jan 27, 2013 at 8:04 PM, Ferrous Cranus <nikos.gr33k@gmail.com> 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 as i want, thats why i ask. The solution you provided me its not correct.
[toc] | [prev] | [next] | [standalone]
| From | rurpy@yahoo.com |
|---|---|
| Date | 2013-01-27 08:12 -0800 |
| Message-ID | <63edba3c-55af-4c4c-a311-4188891bfdf0@googlegroups.com> |
| In reply to | #37762 |
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) )
[toc] | [prev] | [next] | [standalone]
| From | Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-27 10:05 -0800 |
| Message-ID | <f0e08aad-3c69-4ba0-82d0-91652997c7d4@googlegroups.com> |
| In reply to | #37774 |
Τη Κυριακή, 27 Ιανουαρίου 2013 6:12:59 μ.μ. UTC+2, ο χρήστης ru...@yahoo.com έγραψε: > 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?
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2013-01-27 13:36 -0500 |
| Message-ID | <mailman.1116.1359311811.2939.python-list@python.org> |
| In reply to | #37779 |
[Multipart message — attachments visible in raw view] — view raw
On Sun, Jan 27, 2013 at 1:05 PM, Κώστας Παπαδόπουλος
<nikos.gr33k@gmail.com>wrote:
> Τη Κυριακή, 27 Ιανουαρίου 2013 6:12:59 μ.μ. UTC+2, ο χρήστης
> ru...@yahoo.com έγραψε:
> > 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]) )
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
Joel Goldstick
http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-27 10:44 -0800 |
| Message-ID | <68ccd5e6-98b6-4a1d-b991-084e7b7fb459@googlegroups.com> |
| In reply to | #37781 |
Τη Κυριακή, 27 Ιανουαρίου 2013 8:36:42 μ.μ. UTC+2, ο χρήστης Joel Goldstick έγραψε: > On Sun, Jan 27, 2013 at 1:05 PM, Κώστας Παπαδόπουλος <nikos...@gmail.com> wrote: > > Τη Κυριακή, 27 Ιανουαρίου 2013 6:12:59 μ.μ. UTC+2, ο χρήστης ru...@yahoo.com έγραψε: > > > > > > 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.
[toc] | [prev] | [next] | [standalone]
| From | Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-27 10:44 -0800 |
| Message-ID | <mailman.1118.1359312305.2939.python-list@python.org> |
| In reply to | #37781 |
Τη Κυριακή, 27 Ιανουαρίου 2013 8:36:42 μ.μ. UTC+2, ο χρήστης Joel Goldstick έγραψε: > On Sun, Jan 27, 2013 at 1:05 PM, Κώστας Παπαδόπουλος <nikos...@gmail.com> wrote: > > Τη Κυριακή, 27 Ιανουαρίου 2013 6:12:59 μ.μ. UTC+2, ο χρήστης ru...@yahoo.com έγραψε: > > > > > > 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.
[toc] | [prev] | [next] | [standalone]
| From | rurpy@yahoo.com |
|---|---|
| Date | 2013-01-27 11:12 -0800 |
| Message-ID | <d88b7c4c-5576-4477-ae29-c9226ec0e1e4@googlegroups.com> |
| In reply to | #37783 |
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
[toc] | [prev] | [next] | [standalone]
| From | Κώστας Παπαδόπουλος <nikos.gr33k@gmail.com> |
|---|---|
| Date | 2013-01-27 12:24 -0800 |
| Message-ID | <30d46f31-c056-45cf-8902-213536dc11fc@googlegroups.com> |
| In reply to | #37785 |
Τη Κυριακή, 27 Ιανουαρίου 2013 9:12:16 μ.μ. UTC+2, ο χρήστης ru...@yahoo.com έγραψε:
> <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?
[toc] | [prev] | [next] | [standalone]
| From | Mitya Sirenef <msirenef@lightbird.net> |
|---|---|
| Date | 2013-01-27 15:50 -0500 |
| Message-ID | <mailman.1121.1359319842.2939.python-list@python.org> |
| In reply to | #37788 |
On 01/27/2013 03:24 PM, Κώστας Παπαδόπουλος wrote:
> Τη Κυριακή, 27 Ιανουαρίου 2013 9:12:16 μ.μ. UTC+2, ο χρήστης ru...@yahoo.com έγραψε:
>> <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
[toc] | [prev] | [next] | [standalone]
| From | rurpy@yahoo.com |
|---|---|
| Date | 2013-01-27 14:27 -0800 |
| Message-ID | <aa311a2c-4736-4ab0-88bd-00aede2c0f44@googlegroups.com> |
| In reply to | #37789 |
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, ο χρήστης ru...@yahoo.com έγραψε:
> >> <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(...) )
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web