Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)
edited by

I want to create an html table based on filenames. I am having the below files:

apple.good.2.svg

apple.good.1.svg

banana.1.ugly.svg

banana.bad.2.svg

kiwi.good.svg

My output table should look like this:

Object Name    |       good       |       bad       |       ugly

-------------------------------------------------------------------------

apple          | apple.good.1.svg |

               | apple.good.2.svg |

-------------------------------------------------------------------------

banana         |                  | banana.bad.2.svg | banana.1.ugly.svg

-------------------------------------------------------------------------

kiwi           | kiwi.good.svg

-------------------------------------------------------------------------

I have executed the below code:

#!/usr/bin/python

import glob

from collections import defaultdict

fileNames = defaultdict(list)

# fill sorted list of tables based on svg filenames

svgFiles = sorted(glob.glob('*.svg'))

for s in svgFiles: 

    fileNames[s.split('.', 1)[0]].append(s)        

# write to html

html = '<html><table border="1"><tr><th>A</th><th>' + '</th><th>'.join(dict(fileNames).keys()) + '</th></tr>'

for row in zip(*dict(fileNames).values()):

    html += '<tr><td>Object Name</td><td>' + '</td><td>'.join(row) + '</td></tr>'

html += '</table></html>'

file_ = open('result.html', 'w')

file_.write(html)

file_.close()

I managed to read the files sorted in a dict:

{'kiwi': ['kiwi.good.svg'], 'apple': ['apple.good.2.svg', 'apple.good.1.svg'], 'banana': ['banana.1.ugly.svg', 'banana.bad.2.svg']}

But fail by generating the html table.

enter image description here

How can I generate the above html table?  Kindly guide...

1 Answer

0 votes
by (107k points)

You need to iterate all sequences of fruits from your dictionaries and states, and then create one line, not a column for each fruit. Then just repeat all the files resembling that fruit and separate those that hold the current state and join those in one cell.

d = {'kiwi': ['kiwi.good.svg'], 'apple': ['apple.good.2.svg', 'apple.good.1.svg'], 'banana': ['banana.1.ugly.svg', 'banana.bad.2.svg']}

html = """<html><table border="1">

<tr><th>Object</th><th>Good</th><th>Bad</th><th>Ugly</th></tr>"""

for fruit in d:

    html += "<tr><td>{}</td>".format(fruit)

    for state in "good", "bad", "ugly":

        html += "<td>{}</td>".format('<br>'.join(f for f in d[fruit] if ".{}.".format(state) in f))

    html += "</tr>"

html += "</table></html>"

 enter image description here

If you are a beginner and want to know more regrading the same, do refer to the Python certification course.

Related questions

0 votes
4 answers
0 votes
1 answer
asked Feb 7, 2021 in Python by ashely (50.2k points)
0 votes
1 answer
asked Dec 13, 2020 in Python by ashely (50.2k points)
0 votes
1 answer
asked Jul 29, 2019 in Python by Rajesh Malhotra (19.9k points)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...