列表中元素的pandas.to_html()显示新行字符

列表中元素的pandas.to_html()显示新行字符,html,pandas,Html,Pandas,我在一个列表中有一堆需要转换为html表的数据帧。每个数据帧的html代码看起来不错,但是当我将html附加到列表中时,我的网页上会显示一堆\n字符。有人能告诉我怎么摆脱它们吗 python代码: dataframe_html = [] table_dic = {} for df in dataframes: frame = df.to_html(classes="table table-hover") dataframe_html.append(frame) #this is th

我在一个列表中有一堆需要转换为html表的数据帧。每个数据帧的html代码看起来不错,但是当我将html附加到列表中时,我的网页上会显示一堆
\n
字符。有人能告诉我怎么摆脱它们吗

python代码

dataframe_html = []
table_dic = {}

for df in dataframes:
  frame = df.to_html(classes="table table-hover")
  dataframe_html.append(frame)  #this is the line where all the \n get added

table_dic.update({'dataframe_html':dataframe_html})

return render(request,'InterfaceApp/FileProcessor_results.html',table_dic)
<div class="table-responsive">
    {{ dataframe_html | safe }}
</div>
html代码

dataframe_html = []
table_dic = {}

for df in dataframes:
  frame = df.to_html(classes="table table-hover")
  dataframe_html.append(frame)  #this is the line where all the \n get added

table_dic.update({'dataframe_html':dataframe_html})

return render(request,'InterfaceApp/FileProcessor_results.html',table_dic)
<div class="table-responsive">
    {{ dataframe_html | safe }}
</div>

{{dataframe_html | safe}}
显示如下:

"


有人能帮我吗???

要显示3个单独的表,请将HTML字符串列表合并为一个字符串:

dataframe_html = u''.join(dataframe_html)


FWIW在游戏的最后阶段:

我最初有:

    response = render_template('table_display.html', query_results=[df_html], query_name='Quality Item Query')
并且得到了整行\n个字符。更改为“下”,换行符消失

    response = render_template('table_display.html', query_results=df_html, query_name='Quality Item Query')

甚至在游戏后期

我遇到同样的问题时偶然发现了这条线索。希望下面的内容能帮助一些人

我将
df.to_html()
的结果分配给一个(嵌套的)列表,并在我的Jinja模板中渲染时获得新行。受AlliDeacon启发的解决方案是在渲染时再次索引结果

Python代码:

result[0][0] = df.to_html()
Jinja模板:

<div>Table: {{ result[0][0][0] }}</div>
结果:

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th>col_A</th>
      <th>col_B</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>a</td>
      <td>b</td>
    </tr>
  </tbody>
</table>

科鲁阿
科鲁布
A.
B

将其更改为:dataframe\u html.append(frame.strip())@tanjir我尝试了
dataframe\u html.append(frame.strip('\n'))
并且
\n
仍然存在。dataframe\u html.append(frame.strip('u\n'))呢?不过我需要它们是单独的表。您的解决方案生成一个表。tgf,为什么要编辑?这不是重复的html,这是本文的重点。一个版本将正确呈现,另一个版本将在结果页中添加。随着编辑的进行,文章失去了意义。