为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

12345678_0001 2021-10-10 14:37:09
我有以下输入,我需要从中生成带有突出显示文本的 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 webbrowserfrom random import shufflehtmlcolor = ['#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>"#LegendsCOLOR = 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+strhlog_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。但我想以适当的方式生成更好的 html 输出,其中文本之间的行间距,突出显示的文本向左对齐,图例向右对齐。使用 html 设计它可能很容易,但我的要求是使用 python 编码生成它。
查看完整描述

1 回答

?
梦里花落0921

TA贡献1772条经验 获得超6个赞

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')])


查看完整回答
反对 回复 2021-10-10
  • 1 回答
  • 0 关注
  • 154 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号