Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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
Python `pandas.DataFrame.to_html()`不带'table border'和'tr style'`_Python_Html_Pandas - Fatal编程技术网

Python `pandas.DataFrame.to_html()`不带'table border'和'tr style'`

Python `pandas.DataFrame.to_html()`不带'table border'和'tr style'`,python,html,pandas,Python,Html,Pandas,根据标题,是否有可能——如果有,如何——从pandas.DataFrame.to_HTML()生成“干净”的HTML代码 我发现,border=…和justify=…参数控制那里显示的内容,但显然,不管你在那里输入什么值,你似乎总能得到它们 下面是一个简单的工作示例: import pandas as pd import numpy as np df = pd.DataFrame(data=np.arange(3 * 4).reshape(3, 4)) df.to_html(border=0,

根据标题,是否有可能——如果有,如何——从
pandas.DataFrame.to_HTML()
生成“干净”的HTML代码

我发现,
border=…
justify=…
参数控制那里显示的内容,但显然,不管你在那里输入什么值,你似乎总能得到它们

下面是一个简单的工作示例:

import pandas as pd
import numpy as np

df = pd.DataFrame(data=np.arange(3 * 4).reshape(3, 4))
df.to_html(border=0, justify='inherit')
产生:

<table border="0" class="dataframe">
  <thead>
    <tr style="text-align: inherit;">
    ...
将/应产生:

<table class="dataframe">
  <thead>
    <tr>
    ...

...
而不是:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
    ...

...
由于介绍中的
边框
样式
都是外观标签,而不是结构标签,应该通过
css
包含


那么,有没有办法从
表中去除
border
,从
thead
中去除
tr
中的
style

正如您已经观察到的,
df.to_html(class=None,border=None,justify=None)
忽略
None
的设置,插入默认值。有公开的修改请求,但尚未到位。目前,删除这些硬编码样式的唯一方法是操作输出字符串,如下所示:

html = re.sub(r'<tr.*>', '<tr>', df.to_html().replace('border="1" ', ''))
html=re.sub(r'','',df.to_html().replace('border=“1”,'')

删除
class=“dataframe”
可以用同样的方法完成,但如果保留在原位,这不会影响大多数CSS。

这将回答以下问题:如何修改
df.to_html()
的输出,而不是如何使
df.to_html()
产生正确的输出。我只对后者感兴趣。
df.to_html()
不允许您生成正确的输出。删除这些元素的唯一方法是在生成输出后删除它们。并非所有问题都有我们喜欢的答案:-)@norok2打开了一个相应的问题:(pandas 0.23.4)-将
border='0'
传递到
to_html()
函数将成功删除边框。
html = re.sub(r'<tr.*>', '<tr>', df.to_html().replace('border="1" ', ''))