• Uncategorized

About python : pandas-append-multiple-csv-files-into-one-HTML-and-print-csv-file-name

Question Detail

I’m using this script to get 4 html files:


import pandas as pd
import sys
import json
sys.stdout = open('output.html','wt')

df = pd.read_csv('users.csv')

For other file


import pandas as pd
import sys
import json
sys.stdout = open('output1.html','wt')

df = pd.read_csv('groups.csv')

same for 3rd and fourth

These generates 4 tables.

Is it possible to modify code above to get these tables in one html file ?


content of output.html


content of output1.html
same for file 3 and 4

desired output:https://1drv.ms/w/s!AizscpxS0QM4hJo7PSSUarnfy8BEvg

Sorry for doc file, space between 2 tables doesn’t have to be so large, just couldn’t reduce it

The goal is to send it as HTML email body, i can send it separately but can’t figure out how to create a space between tables and to add some description for every table, so don’t know is it possible to combine it in one big HTML file and to add space between tables and some text

Question Answer

A short experiment shows that pd.to_html just does an HTML table, not a full HTML document:

import pandas as pd

>>> pd.DataFrame({'a': [1, 2]}).to_html()
'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>a</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>'

Any concatenation of the resulting HTMLs, therefore, possibly divided into divs, will work. For example:

'<div>%s</div>\n<div>%s</div>' % (open('output.html').read(), open('output`.html').read())

will concatenate the first two tables, each in its own div.

You may also like...

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.