Skip to content

Instantly share code, notes, and snippets.

@quandyfactory
Created June 23, 2010 15:40
Show Gist options
  • Save quandyfactory/450099 to your computer and use it in GitHub Desktop.
Save quandyfactory/450099 to your computer and use it in GitHub Desktop.
Makes an HTML table.
def make_table(data, delim='\t', table_id='make_table', classname=''):
"""
Converts delimited data into an HTML table.
- `data` is a string, with rows delimited by newlines.
- Default cell delimiter for each row is a tab.
"""
output = []
output.append('<table id="%s" class="%s">' % (table_id, classname))
for row in data.strip().split('\n'):
cells = row.strip().split(delim)
tag = 'td'
if data.strip().split('\n').index(row) == 0:
tag = 'th'
output.append(' <thead>')
output.append(' <tr>')
for cell in cells:
output.append(' <%s>%s</%s>' % (tag, cell.strip(), tag))
output.append(' </tr>')
if data.strip().split('\n').index(row) == 0:
output.append(' </thead>')
output.append(' <tbody>')
output.append(' </tbody>')
output.append('</table>')
return '\n'.join(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment