我将链接嵌入 Pandas 数据帧的一列(下表)并将数据帧写入 hmtl。
数据帧表中的链接格式如下(索引表中的第一个链接):
In: table.loc[0,'Links']
Out: u'<a href="http://xxx.xx.xxx.xxx/browser/I6.html">I6</a>'
如果我查看(而不是索引特定行)数据框(在笔记本中),链接文本将被截断:
<a href="http://xxx.xx.xxx.xxx/browser/I6.html...
我将数据框写入 html:
table_1=table.to_html(classes='table',index=False,escape=False)
但是,截断的链接(而不是全文)被写入 html 表:
<td> <a href="http://xxx.xx.xxx.xxx/browser/I6.html...</td>\n
我可能需要 to_html() 的附加参数。
现在查看文档,但建议如下:
http://pandas.pydata.org/pandas-docs/dev/ generated/pandas.DataFrame.to_html.html
Thanks!
因此,可能有一个特定于 pandas 的解释,但您也可以通过以下方式解决该问题:(a) 用键值替换链接,(b) 编写 html 表字符串,然后 (c) 用适当的值替换键链接。
例如,用一个键替换每个链接,将键存储在字典中:
map = {}
for i in df.index:
counter = 0
if df.ix[i]['Links'] in map:
df.ix[i, 'Links'] = map[df.ix[i]['Links']]
else:
map[df.ix[i, 'Links']] = 'href' + str(counter)
counter += 1
df.ix[i, 'Links'] = map[df.ix[i]['Links']]
写表:
table_1 = df.to_html(classes='table',index=False,escape=False)
重写链接:
for key, value in map.iteritems():
table_1 = table_1.replace(value, key)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)