Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [OrderedDict([('Name', 'Soytra'), ('Class', 'First'), ('Number', '23768'), ('Place', 'NY'), ('Place1', 'LA'), ('Grade', 16.6038)]), OrderedDict([('Name', 'Reuhyta'), ('Class', 'First'), ('Number', '23769'), ('Place', 'NY'), ('Place1', 'LA'), ('Grade', 23.56789)])]
- In [4]: l = [OrderedDict([('Name', 'Soytra'), ('Class', 'First'), ('Number', '23768'), ('Place', 'NY'), ('Place1', 'LA'
- ...: ), ('Grade', 16.6038)]), OrderedDict([('Name', 'Reuhyta'), ('Class', 'First'), ('Number', '23769'), ('Place', '
- ...: NY'), ('Place1', 'LA'), ('Grade', 23.56789)])]
- In [13]: df = pd.DataFrame(l)
- In [14]: df
- Out[14]:
- Name Class Number Place Place1 Grade
- 0 Soytra First 23768 NY LA 16.60380
- 1 Reuhyta First 23769 NY LA 23.56789
- In [15]: df.to_html()
- Out[15]: '<table border="1" class="dataframe">n <thead>n <tr style="text-align: right;">n <th></th>n <th>Name</th>n <th>Class</th>n <th>Number</th>n <th>Place</th>n <th>Place1</th>n <th>Grade</th>n </tr>n </thead>n <tbody>n <tr>n <th>0</th>n <td>Soytra</td>n <td>First</td>n <td>23768</td>n <td>NY</td>n <td>LA</td>n <td>16.60380</td>n </tr>n <tr>n <th>1</th>n <td>Reuhyta</td>n <td>First</td>n <td>23769</td>n <td>NY</td>n <td>LA</td>n <td>23.56789</td>n </tr>n </tbody>n</table>'
- # convert the ordered dicts into a python list
- keys, rows = [], []
- for sub_dict in ordered_items:
- row = []
- for key in sub_dict:
- if key not in keys:
- keys.append(key)
- row.append(sub_dict[key])
- rows.append(row)
- # convert the python list into a HTML table
- thead = "<thead>n {}</thead>".format("".join(map(lambda key: "<th>{}</th>".format(key), keys)))
- tbody = "<tbody>"
- for row in rows:
- tbody += "n <tr>{}</tr>".format("".join(map(lambda value: "<td>{}</td>".format(value), row)))
- tbody += "</tbody>"
- print("<table>" + thead + tbody + "</table>")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement