【问题标题】:How to use variable in triple quote raw text in python?如何在python中的三引号原始文本中使用变量?
【发布时间】:2022-07-06 05:03:30
【问题描述】:

我有 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: not enough arguments for format string 错误。

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

这里如何使用变量?

【问题讨论】:

  • “给出一个错误”或“显示一个错误”这些词永远不能不伴随有问题的错误。
  • 另外,您当然可以构建一个minimal reproducible example,只用一位数的行数就可以产生同样的问题吗?
  • @CharlesDuffy 我已经包含了一个最小可重复的示例以及我还包含了what I tried 我还应该包含什么?

标签: python python-3.x f-string


【解决方案1】:

您可以简单地将 attention_vector 的内容连接到一个字符串,然后使用 """ + attention 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
}]"""

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)

【讨论】:

    猜你喜欢
    • 2015-09-30
    • 2021-10-12
    • 2011-06-05
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多