Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将iPython HTML类发送到.HTML文件?_Python_Python 3.x_Pandas_Ipython - Fatal编程技术网

如何将iPython HTML类发送到.HTML文件?

如何将iPython HTML类发送到.HTML文件?,python,python-3.x,pandas,ipython,Python,Python 3.x,Pandas,Ipython,我有一个对象,class'IPython.core.display.HTML,我正试图将其保存为一个.HTML文件。我该怎么做 def HTML_with_style(df, style=None, random_id=None): # https://stackoverflow.com/questions/38511373/change-the-color-of-text-within-a-pandas-dataframe-html-table-python-using-style/3

我有一个对象,
class'IPython.core.display.HTML
,我正试图将其保存为一个
.HTML
文件。我该怎么做

def HTML_with_style(df, style=None, random_id=None):
    # https://stackoverflow.com/questions/38511373/change-the-color-of-text-within-a-pandas-dataframe-html-table-python-using-style/38511805#38511805
    from IPython.display import HTML
    import numpy as np
    import re

    df_html = df.to_html()

    if random_id is None:
        random_id = 'id%d' % np.random.choice(np.arange(1000000))

    if style is None:
        style = """
        <style>
            table#{random_id} {{color: blue}}
        </style>
        """.format(random_id=random_id)
    else:
        new_style = []
        s = re.sub(r'</?style>', '', style).strip()
        for line in s.split('\n'):
                line = line.strip()
                if not re.match(r'^table', line):
                    line = re.sub(r'^', 'table ', line)
                new_style.append(line)
        new_style = ['<style>'] + new_style + ['</style>']

        style = re.sub(r'table(#\S+)?', 'table#%s' % random_id, '\n'.join(new_style))

    df_html = re.sub(r'<table', r'<table id=%s ' % random_id, df_html)

    return HTML(style + df_html)


new_df = HTML_with_style(df) # df is a Pandas DataFrame
f = open("test.html", 'w')
f.write(display(new_df))
def HTML_with_style(df,style=None,random_id=None):
# https://stackoverflow.com/questions/38511373/change-the-color-of-text-within-a-pandas-dataframe-html-table-python-using-style/38511805#38511805
从IPython.display导入HTML
将numpy作为np导入
进口稀土
df_html=df.to_html()
如果random_id为None:
random_id='id%d'%np.random.choice(np.arange(1000000))
如果样式为“无”:
style=”“”
表#{random_id}{{color:blue}
“”格式(随机id=随机id)
其他:
新样式=[]
s=re.sub(r'','',样式).strip()
对于s.split('\n')中的行:
line=line.strip()
如果未重新匹配(r'^table',行):
行=re.sub(r'^',表',行)
新样式。追加(行)
新样式=['']+新样式+[']
style=re.sub(r'table(#\S+),'table#%S'%random\u id',\n'.join(新样式))

df_html=re.sub(r'似乎可以替换

return HTML(style + df_html)

并执行以下操作:

with open('/path/to/file.html', 'w') as f:
    f.write(HTML_with_style(df))
但是,也许你应该寻找更复杂的样式

我用我的更正测试了您的功能,保存了一个测试数据框&它在我的浏览器中显示为蓝色表格,如下所示,我相信它应该是这样的:


天哪,真管用!聪明的想法。另外,感谢熊猫链接。它看起来有点复杂,但我肯定会研究它,所以我不必用(IMO)这种笨拙的方式来做。
with open('/path/to/file.html', 'w') as f:
    f.write(HTML_with_style(df))