如何在Python中使用三重引用的原始文本中使用变量?
我有HTML文本,我想在该原始HTML文本中使用变量。
文本看起来像这样:
js_getResults =""" <!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
type="text/javascript"
src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"
></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style id="compiled-css" type="text/css">
/* EOS */
</style>
<script id="insert"></script>
</head>
<body>
<div id="text">text goes here</div>
<script type="text/javascript">//<![CDATA[
var words = data_goes_here;
$('#text').html($.map(words, function(w) {
return '<span style="background-color:hsl(360,100%,' + (w.attention * 50 + 50) + '%)">' + w.word + ' </span>'
}))
//]]></script>
<script>
// tell the embed parent frame the height of the content
if (window.parent && window.parent.parent){
window.parent.parent.postMessage(["resultsFrame", {
height: document.body.getBoundingClientRect().height,
slug: "ohLs4ae0"
}], "*")
}
// always overwrite window.name, in case users try to set it manually
window.name = "result"
</script>
</body>
</html> """
我想用列表替换data_goes_here。
我尝试的是:
我尝试使用f-string
,但它给出了一个错误:
js_getResults = f'''<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
type="text/javascript"
src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"
></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style id="compiled-css" type="text/css">
/* EOS */
</style>
<script id="insert"></script>
</head>
<body>
<div id="text">text goes here</div>
<script type="text/javascript">//<![CDATA[
var words = {attention_vector};
$('#text').html($.map(words, function(w) {
return '<span style="background-color:hsl(360,100%,' + (w.attention * 50 + 50) + '%)">' + w.word + ' </span>'
}))
//]]></script>
<script>
// tell the embed parent frame the height of the content
if (window.parent && window.parent.parent){
window.parent.parent.postMessage(["resultsFrame", {
height: document.body.getBoundingClientRect().height,
slug: "ohLs4ae0"
}], "*")
}
// always overwrite window.name, in case users try to set it manually
window.name = "result"
</script>
</body>
</html>'''
我还尝试了%s
,但它没有奏效,并且给出typeerror:不够格式字符串的参数
错误。
coation_vector看起来像这样:
attention_vector = [{
'word': 'Lorem',
'attention': 0.39
}, {
'word': 'ipsum',
'attention': 0.76
}, {
'word': 'dolor',
'attention': 0.2
}, {
'word': 'sit',
'attention': 0.43
}, {
'word': 'amet,',
'attention': 0.54
}, {
'word': 'consectetur',
'attention': 0.29
}, {
'word': 'adipiscing',
'attention': 0.98
}]
如何在此处使用变量?
I have HTML text and I want to use variables in that raw HTML text.
The text looks like this:
js_getResults =""" <!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
type="text/javascript"
src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"
></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style id="compiled-css" type="text/css">
/* EOS */
</style>
<script id="insert"></script>
</head>
<body>
<div id="text">text goes here</div>
<script type="text/javascript">//<![CDATA[
var words = data_goes_here;
$('#text').html($.map(words, function(w) {
return '<span style="background-color:hsl(360,100%,' + (w.attention * 50 + 50) + '%)">' + w.word + ' </span>'
}))
//]]></script>
<script>
// tell the embed parent frame the height of the content
if (window.parent && window.parent.parent){
window.parent.parent.postMessage(["resultsFrame", {
height: document.body.getBoundingClientRect().height,
slug: "ohLs4ae0"
}], "*")
}
// always overwrite window.name, in case users try to set it manually
window.name = "result"
</script>
</body>
</html> """
I want to replace data_goes_here with a list.
What I tried:
I tried to use f-string
but it's giving an error:
js_getResults = f'''<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title></title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="robots" content="noindex, nofollow">
<meta name="googlebot" content="noindex, nofollow">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script
type="text/javascript"
src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"
></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<style id="compiled-css" type="text/css">
/* EOS */
</style>
<script id="insert"></script>
</head>
<body>
<div id="text">text goes here</div>
<script type="text/javascript">//<![CDATA[
var words = {attention_vector};
$('#text').html($.map(words, function(w) {
return '<span style="background-color:hsl(360,100%,' + (w.attention * 50 + 50) + '%)">' + w.word + ' </span>'
}))
//]]></script>
<script>
// tell the embed parent frame the height of the content
if (window.parent && window.parent.parent){
window.parent.parent.postMessage(["resultsFrame", {
height: document.body.getBoundingClientRect().height,
slug: "ohLs4ae0"
}], "*")
}
// always overwrite window.name, in case users try to set it manually
window.name = "result"
</script>
</body>
</html>'''
I also tried %s
but it's not working out and giving TypeError: not enough arguments for format string
error.
attention_vector looks like this:
attention_vector = [{
'word': 'Lorem',
'attention': 0.39
}, {
'word': 'ipsum',
'attention': 0.76
}, {
'word': 'dolor',
'attention': 0.2
}, {
'word': 'sit',
'attention': 0.43
}, {
'word': 'amet,',
'attention': 0.54
}, {
'word': 'consectetur',
'attention': 0.29
}, {
'word': 'adipiscing',
'attention': 0.98
}]
How to use variable here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许是因为您的
f-string
被HTML/JS中的卷发括号混淆。请参阅此答案您需要将
{{
和}}}
在您的HTML/JS中加倍。Perhaps it's because your
f-string
is confused by the curly braces in your HTML/JS. See this answerYou need to double the
{{
and}}
in your HTML/JS.您可以简单地将注意力图的内容串联到字符串中,然后使用
“”“ +注意向量 +”“”
将其粘贴到位:You could simply concatenate the contents of your attention_vector to a string and then use
""" + attention vector + """
to paste it into place: