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


Groups > comp.lang.python > #78246

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

From Denis McMahon <denismfmcmahon@gmail.com>
Newsgroups comp.lang.python
Subject Re: how to write a html to automatically display the dictionary?
Date 2014-09-23 20:53 +0000
Organization A noiseless patient Spider
Message-ID <lvsmkm$kal$2@dont-email.me> (permalink)
References <mailman.14262.1411481932.18130.python-list@python.org> <lvsavt$h7m$1@reader1.panix.com>

Show all headers | View raw


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

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web