我正在尝试通过yattag创建html。唯一的问题是,我从另一个html文件中使用的头文件,所以我读取该文件,并尝试将其作为头文件插入。这是问题所在。虽然我传递了未转义的html字符串,但yattag对其进行了转义。也就是说,它将'<‘转换为<,同时将其添加到html字符串。
MWE:
from yattag import Doc, indent
import html
doc, tag, text = Doc().tagtext()
h = open(nbheader_template, 'r')
h_content= h.read()
h_content = html.unescape(h_content)
doc.asis('<!DOCTYPE html>')
with tag('html'):
# insert dummy head
with tag('head'):
text(h_content) # just some dummy text to replace later - workaround for now
with tag('body'):
# insert as many divs as no of files
for i in range(counter):
with tag('div', id = 'divID_'+ str(1)):
text('Div Page: ' + str(i))
result = indent(doc.getvalue())
# inject raw head - dirty workaround as yattag not doing it
# result = result.replace('<head>headtext</head>',h_content)
with open('test.html', "w") as file:
file.write(result)输出:

上下文:我正在尝试将多个jupyter python笔记本合并到一个html中,这就是为什么header很重。可以在here中找到报头内容(nbheader_template
发布于 2018-12-05 04:17:33
我对'yattag‘并不是很流利,但有一件事我看不懂,那就是:
with tag('body'):您引用的代码(上面)是将<div>和文本放入标题中,而这些内容显然不属于您的标题。
https://stackoverflow.com/questions/53619083
复制相似问题