Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #78213 > unrolled thread

how to write a html to automatically display the dictionary?

Started byluofeiyu <elearn2014@gmail.com>
First post2014-09-23 22:18 +0800
Last post2014-09-23 19:55 -0400
Articles 6 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  how to write a html to automatically display  the dictionary? luofeiyu <elearn2014@gmail.com> - 2014-09-23 22:18 +0800
    Re: how to write a html to automatically display  the dictionary? Tobiah <toby@tobiah.org> - 2014-09-23 09:53 -0700
      Re: how to write a html to automatically display  the dictionary? Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-23 20:20 +0000
    Re: how to write a html to automatically display the dictionary? John Gordon <gordon@panix.com> - 2014-09-23 17:34 +0000
      Re: how to write a html to automatically display the dictionary? Denis McMahon <denismfmcmahon@gmail.com> - 2014-09-23 20:53 +0000
        Re: how to write a html to automatically display the dictionary? Ned Batchelder <ned@nedbatchelder.com> - 2014-09-23 19:55 -0400

#78213 — how to write a html to automatically display the dictionary?

Fromluofeiyu <elearn2014@gmail.com>
Date2014-09-23 22:18 +0800
Subjecthow to write a html to automatically display the dictionary?
Message-ID<mailman.14262.1411481932.18130.python-list@python.org>
x={'f1':1,'f2':2,'f3':3}
how can i create the following html file automatically with python to 
display x ?

<table border=1>
<tr>
<td>
f1
</td>
<td>
f2
</td>
<td>
f3
</td>
</tr>
<td>
1
</td>
<td>
2
</td>
<td>
3
</td>
<tr>
</tr>
</table>

[toc] | [next] | [standalone]


#78227

FromTobiah <toby@tobiah.org>
Date2014-09-23 09:53 -0700
Message-ID<5421A594.8030604@tobiah.org>
In reply to#78213
On 09/23/2014 07:18 AM, luofeiyu wrote:
> x={'f1':1,'f2':2,'f3':3}
> how can i create the following html file automatically with python to display x ?
>
> <table border=1>
> <tr>
> <td>
> f1
> </td>
> <td>
> f2
> </td>
> <td>
> f3
> </td>
> </tr>
> <td>
> 1
> </td>
> <td>
> 2
> </td>
> <td>
> 3
> </td>
> <tr>
> </tr>
> </table>

def tablefy(values):
         print "<tr>"
         for value in values:
                 print "<td>%s</td>" % value
         print "</tr>"

x = {'f1': 1,'f2': 2,'f3': 3}

print "<table border=1>"
tablefy(x.keys())
tablefy(x.values())
print "</table>"

[toc] | [prev] | [next] | [standalone]


#78245

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-09-23 20:20 +0000
Message-ID<lvskmt$kal$1@dont-email.me>
In reply to#78227
On Tue, 23 Sep 2014 09:53:40 -0700, Tobiah wrote:

> On 09/23/2014 07:18 AM, luofeiyu wrote:

>> how can i create the following html

>> f3 </td>
>> </tr>

No <tr> here?

>> <td>
>> 1

...

>> <td>
>> 3
>> </td>
>> <tr>

What is the above <tr> doing there?

>> </tr>
>> </table>

> [code]

Although your solution will produce valid html, it doesn't produce the 
specified output. ;)

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#78232 — Re: how to write a html to automatically display the dictionary?

FromJohn Gordon <gordon@panix.com>
Date2014-09-23 17:34 +0000
SubjectRe: how to write a html to automatically display the dictionary?
Message-ID<lvsavt$h7m$1@reader1.panix.com>
In reply to#78213
In <mailman.14262.1411481932.18130.python-list@python.org> luofeiyu <elearn2014@gmail.com> writes:

> x={'f1':1,'f2':2,'f3':3}
> how can i create the following html file automatically with python to 
> display x ?

You might want to use something other than a dictionary, as the order
isn't guaranteed.

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [next] | [standalone]


#78246 — Re: how to write a html to automatically display the dictionary?

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-09-23 20:53 +0000
SubjectRe: how to write a html to automatically display the dictionary?
Message-ID<lvsmkm$kal$2@dont-email.me>
In reply to#78232
On Tue, 23 Sep 2014 17:34:53 +0000, John Gordon wrote:

> In <mailman.14262.1411481932.18130.python-list@python.org> luofeiyu
> <elearn2014@gmail.com> writes:
> 
>> x={'f1':1,'f2':2,'f3':3}
>> how can i create the following html file automatically with python to
>> display x ?
> 
> You might want to use something other than a dictionary, as the order
> isn't guaranteed.

Assuming you want them in order of the key field:

from string import *

x={'f1':1,'f2':2,'f3':3}
y = [ (a,x[a]) for a in x.keys() ]
y.sort( cmp=lambda a,b: cmp(a[0],b[0]) )
table = ( "<table>\n" +
          "\n".join(["<tr>\n" +
                     "\n".join(["<td>{}</td>".format(z[i]) for z in y]) +
                     "\n</tr>" for i in range(len(y[0]))]) +
          "\n</table>" )
print table

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#78261 — Re: how to write a html to automatically display the dictionary?

FromNed Batchelder <ned@nedbatchelder.com>
Date2014-09-23 19:55 -0400
SubjectRe: how to write a html to automatically display the dictionary?
Message-ID<mailman.14297.1411516565.18130.python-list@python.org>
In reply to#78246
On 9/23/14 4:53 PM, Denis McMahon wrote:
> from string import *

You aren't using any names from string, so you can skip this line.

>
> x={'f1':1,'f2':2,'f3':3}
> y = [ (a,x[a]) for a in x.keys() ]
> y.sort( cmp=lambda a,b: cmp(a[0],b[0]) )

This is more easily done as:

     y = sorted(x.items())

.items() returns a sequence of (key, value) pairs, and tuples are sorted 
lexicographically, so the first elements are compared first, then the 
second, etc.

-- 
Ned Batchelder, http://nedbatchelder.com

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web