簡體   English   中英

如何將帶有圖例的突出顯示文本作為 html 輸出?

[英]How to get highlighted text with legends as html output?

我有以下輸入,我需要從中生成帶有突出顯示文本的 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。

圖片截圖

但我想以適當的方式生成更好的 html 輸出,其中文本之間的行間距,突出顯示的文本向左對齊,圖例向右對齊。

使用 html 設計它可能很容易,但我的要求是使用 python 編碼生成它。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM