0

I have a csv file and I want to generated some data out of this file. The issue is that I can't find any easy, straight forward way in order to generate a customized template, I know that there are many tools like jinja and cheetah where I can use the templates directly, But what I am looking for a more simple way where I have some data and I want to generate it in HTML report format.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
BlackHawk
  • 37
  • 8
  • 2
    Put the solution as an answer and leave the question as such – Bhargav Rao Apr 21 '15 at 13:37
  • So how I can change it from question to answer. I'm fairly new here – BlackHawk Apr 21 '15 at 13:41
  • I'm sorry, but I'm not sure that *"Python has a `str.format`"* makes a good answer - this is well covered in most tutorials (e.g. see link in https://docs.python.org/2/tutorial/introduction.html#strings). See also *"you need to open files to write to them"* and *"you can do things with `for` loops"*. There are also questions like http://stackoverflow.com/q/11764900/3001761 – jonrsharpe Apr 21 '15 at 13:43
  • @BlackHawk Copy the answer part, Click on *Post your answer* and then past it there and finally click post – Bhargav Rao Apr 21 '15 at 13:48

1 Answers1

0

String format is really flexible tool where you can use the if conditions and for loop with no issue example :

All you need to do is to open a file in order to redirect the html tags to it:

outfile = open("output/report.html", "w+")

then you can use string format to replace the variables inside ur code with their values:

   name = "Humtty"

print >>outfile,"""<div><h4>Funny Name {name}</h4></div>""".format(**locals())

for loops are also supported over lists and dicts:

for index, elem in enumerate(LONG_LIST):
 print >>outfile,"""


<tr>
<td  align="center">{0}</td>
<td  align="center">{1}</td>
<td  align="center">{2}</td>
</tr>""".format(elem[0], elem[1], elem[2])
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
BlackHawk
  • 37
  • 8