Created
June 23, 2010 15:40
-
-
Save quandyfactory/450099 to your computer and use it in GitHub Desktop.
Makes an HTML table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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