如何在Python中使用三重引用的原始文本中使用变量?

发布于 2025-02-13 04:57:28 字数 3919 浏览 1 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帅冕 2025-02-20 04:57:30

也许是因为您的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 answer

You need to double the {{ and }} in your HTML/JS.

箜明 2025-02-20 04:57:29

您可以简单地将注意力图的内容串联到字符串中,然后使用“”“ +注意向量 +”“”将其粘贴到位:

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
}]"""

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 = """ + 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> """

print(js_getResults)

You could simply concatenate the contents of your attention_vector to a string and then use """ + attention vector + """ to paste it into place:

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
}]"""

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 = """ + 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> """

print(js_getResults)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文