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

Python 如何将带有图例的突出显示文本作为html输出?,python,html,Python,Html,我有下面的输入,我需要从中生成html输出,突出显示的文本与每种颜色的图例 inp = [('Python', 'language'), ('is', 'others'), ('a', 'others'), ('programming', 'others'), ('language', 'others'), ('.', 'others'), ('John', 'Person'), ('is', 'others'), ('an', 'others'), ('excellent', 'Modifie

我有下面的输入,我需要从中生成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生成一个好的基本html输出,而不使用CSS或JS脚本

我编写了以下代码

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)
def颜色(输出):
导入网络浏览器
从随机导入洗牌
HtmlCOLOHTHTLCLOR=[“零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零D59','#7E354D']
洗牌(htmlcolor)
clt=[]
对于i,j in out:
如果j!='其他人:
附加条款(j)
颜色=htmlcolor[:len(clt)]
d=dict(拉链(clt,彩色))
strh=“”
对于i,j in out:
如果j==‘其他人’:
strh+=i
strh+=“”
如果j!='其他人:
strh+=“%d[j]
strh+=i
strh+=“”
strh+=“”
strh+=“”
#传说
颜色=颜色[:len(clt)]
lang=['language'、'Person'、'location']
cl=列表(zip(颜色、语言))
stri=''
stri+=“”
对于j,i在d.items()中:
stri+=''
stri+=“
def颜色(输出):
导入网络浏览器
从随机导入洗牌
HtmlCOLOHTHTLCLOR=[“零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零零D59','#7E354D']
洗牌(htmlcolor)
clt=[]
对于i,j in out:
如果j!=“其他人”:
附加条款(j)
颜色=htmlcolor[:len(clt)]
d=dict(拉链(clt,彩色))
strh=“”
对于i,j in out:
如果j==‘其他人’:
strh+=i
strh+=“”
如果j!=“其他人”:
strh+=“%d[j]
strh+=i
strh+=“”
strh+=“”
strh+=“”
#传说
颜色=颜色[:len(clt)]
lang=['language'、'Person'、'location']
cl=列表(zip(颜色、语言))
stri=''
stri+=“”
对于j,i在d.items()中:
stri+=''
stri+=“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuui”
stri+=%s”%j
stri+=''
stri+=“”
新=stri+strh
log_file=open('path/testcasec3.html','w')
日志文件。写入(新)
返回webbrowser.open('path/testcasec3.html')
nercolor([('Python','language'),
(‘是’、‘其他’),
(‘a’、‘其他’),
(“编程”、“其他”),
(“语言”、“其他”),
(“,”其他“),
(“约翰”,“人”),
(‘是’、‘其他’),
(‘安’、‘其他’),
(‘极好’、‘修饰语’),
(“编码员”、“其他人”),
(“基于”、“行动”),
(“out”、“others”),
(“of”、“others”),
(‘加利福尼亚’、‘地点’),
(“,”其他“)])

出于好奇;您在上面使用HTML,您在哪里划定了使用HTML的界限。我的意思是我们如何知道何时使用?另外,您是否考虑过输出图像?这样您就可以完全避免使用HTML。您可以用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><div style='width:70%;display:inline-block'>"
    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 += "</div></html>"

    #Legends

    COLOR = COLOR[:len(clt)]
    lang = ['language', 'Person', 'location']
    cl = list(zip(COLOR, lang))

    stri = '<div style="width:20%;display:inline-block">'
    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></div>"

    new = stri+strh
    log_file = open('path/testcasecc3.html', 'w')
    log_file.write(new)
    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')])