如何将带有图例的突出显示文本作为 html 输出?

我有以下输入,我需要从中生成带有突出显示文本的 html 输出,每种颜色都有图例。


inp = [('Python', 'language'),

('is', 'others'),

('a', 'others'),

('programming', 'others'),

('language', 'others'),

('.', 'others'),

('John', 'Person'),

('is', 'others'),

('an', 'others'),

('excellent', 'Modifier'),

('coder', 'others'),

('based', 'Action'),

('out', 'others'),

('of', 'others'),

('California', 'location'),

('.', 'others')]

我想通过使用 Python 而不使用 CSS 或 JS 脚本来生成一个很好的基本 html 输出。


我写了以下代码。


def nercolor(out):

import webbrowser

from random import shuffle


htmlcolor = ['#00FFFF', '#FF0000', '#ADD8E6', '#00FF00', '#FF00FF', '#FFA500', '#008000', '#808000', '#736AFF', '#368BC1', '#95B9C7', '#7FFFD4', '#728C00', '#FFA62F', '#827839', '#C36241', '#F75D59', '#7E354D']


shuffle(htmlcolor)

clt = []

for i,j in out:

    if j != 'others':

        clt.append(j)


COLOR = htmlcolor[:len(clt)]

d = dict(zip(clt, COLOR))


strh = "<html>"

for i,j in out:

    if j == 'others':

        strh += i

        strh += " "

    if j != 'others':

        strh += "<strong><span style='background-color:%s'>" % d[j]

        strh += i

        strh += "</span></strong>"

        strh += " "

strh += "</html>"


#Legends


COLOR = COLOR[:len(clt)]

lang = ['language', 'Person', 'location']

cl = list(zip(COLOR, lang))


stri = ''

stri += "<table>"

for j, i in d.items():

    stri += '<tr>'

    stri += "<td <span style='background-color: %s'>__________</span>&nbsp;</td>" %i

    stri += "<td>%s</td>" %j

    stri += '</tr>'

stri += "</table>"


new = stri+strh

log_file = open('/home/nms2kor/Documents/graphs/testcasecc3.html', 'w')

log_file.write(new)

return webbrowser.open('/home/nms2kor/Documents/graphs/testcasecc3.html')


nercolor(result)

我从我的代码中得到了以下输出 html。

http://img2.mukewang.com/61628a2c0001f2f807650181.jpg

但我想以适当的方式生成更好的 html 输出,其中文本之间的行间距,突出显示的文本向左对齐,图例向右对齐。

使用 html 设计它可能很容易,但我的要求是使用 python 编码生成它。


12345678_0001
浏览 156回答 1
1回答

梦里花落0921

def nercolor(out):&nbsp; &nbsp; import webbrowser&nbsp; &nbsp; from random import shuffle&nbsp; &nbsp; htmlcolor = ['#00FFFF', '#FF0000', '#ADD8E6', '#00FF00', '#FF00FF', '#FFA500', '#008000', '#808000', '#736AFF', '#368BC1', '#95B9C7', '#7FFFD4', '#728C00', '#FFA62F', '#827839', '#C36241', '#F75D59', '#7E354D']&nbsp; &nbsp; shuffle(htmlcolor)&nbsp; &nbsp; clt = []&nbsp; &nbsp; for i,j in out:&nbsp; &nbsp; &nbsp; &nbsp; if j != 'others':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clt.append(j)&nbsp; &nbsp; COLOR = htmlcolor[:len(clt)]&nbsp; &nbsp; d = dict(zip(clt, COLOR))&nbsp; &nbsp; strh = "<html><div style='width:70%;display:inline-block'>"&nbsp; &nbsp; for i,j in out:&nbsp; &nbsp; &nbsp; &nbsp; if j == 'others':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strh += i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strh += " "&nbsp; &nbsp; &nbsp; &nbsp; if j != 'others':&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strh += "<strong><span style='background-color:%s'>" % d[j]&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strh += i&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strh += "</span></strong>"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; strh += " "&nbsp; &nbsp; strh += "</div></html>"&nbsp; &nbsp; #Legends&nbsp; &nbsp; COLOR = COLOR[:len(clt)]&nbsp; &nbsp; lang = ['language', 'Person', 'location']&nbsp; &nbsp; cl = list(zip(COLOR, lang))&nbsp; &nbsp; stri = '<div style="width:20%;display:inline-block">'&nbsp; &nbsp; stri += "<table>"&nbsp; &nbsp; for j, i in d.items():&nbsp; &nbsp; &nbsp; &nbsp; stri += '<tr>'&nbsp; &nbsp; &nbsp; &nbsp; stri += "<td><span style='background-color: %s'>__________</span>&nbsp;</td>" %i&nbsp; &nbsp; &nbsp; &nbsp; stri += "<td>%s</td>" %j&nbsp; &nbsp; &nbsp; &nbsp; stri += '</tr>'&nbsp; &nbsp; stri += "</table></div>"&nbsp; &nbsp; new = stri+strh&nbsp; &nbsp; log_file = open('path/testcasecc3.html', 'w')&nbsp; &nbsp; log_file.write(new)&nbsp; &nbsp; return webbrowser.open('path/testcasecc3.html')nercolor([('Python', 'language'),('is', 'others'),('a', 'others'),('programming', 'others'),('language', 'others'),('.', 'others'),('John', 'Person'),('is', 'others'),('an', 'others'),('excellent', 'Modifier'),('coder', 'others'),('based', 'Action'),('out', 'others'),('of', 'others'),('California', 'location'),('.', 'others')])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python