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.
How can I generate the above html table? Kindly guide...