首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在python中将代码块捕获到字符串变量中

在python中将代码块捕获到字符串变量中
EN

Stack Overflow用户
提问于 2018-07-22 05:07:38
回答 1查看 297关注 0票数 0

我想动态创建一个html代码块,但我不确定如何将其赋给变量:

代码语言:javascript
复制
<head>
<style>
body {
    border-style: solid;
    border-width: 5px;
    border-color: green;
    border-radius: 5px;
    border-spacing: 30px;
    background-color: #FFFFC2;
    padding-bottom: 5px;
    font-size: 25px;
}

li {
    color: #F7270F;
    font-weight: bold;
}
</style>
</head>
<body>
<div style="margin-bottom: 5px; margin-top: 5px; margin-left: 10px; margin-right: 10px;">
<h1 align="center">Auctions for Week of 08-01-2018</h1>
<p>You are bidding on the following item:</p>
<ul><li>This is the item for sale</li></ul>
<p>Condition is pack fresh unless otherwise indicated. Please review the pictures carefully and if you have any questions about something specific, ask.</p>
<p><b>Shipping:</b> Shipping will be calculated based on buyer location. No premiums are charged. Cards are mailed in an 8x5 bubble mailer and shipped First Class mail unless the price exceeds $50, at which point they will be shipped Priority at no additional cost to the buyer. If you win multiple auctions, please wait for an invoice to be sent.</p>
</div>
</body>

我试过执行以下命令:

html_desc="<code_block>"

但它不喜欢它的工作方式...所以我有点被难住了。我尝试将其放入变量的原因是,我需要将其作为JSON语句的键值。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-22 05:27:12

您可以将该代码块放入三重引号"""中(例如,我将变量放入json.dumps中):

代码语言:javascript
复制
from pprint import pprint
import json

html_desc = """<head>
<style>
body {
    border-style: solid;
    border-width: 5px;
    border-color: green;
    border-radius: 5px;
    border-spacing: 30px;
    background-color: #FFFFC2;
    padding-bottom: 5px;
    font-size: 25px;
}

li {
    color: #F7270F;
    font-weight: bold;
}
</style>
</head>
<body>
<div style="margin-bottom: 5px; margin-top: 5px; margin-left: 10px; margin-right: 10px;">
<h1 align="center">Auctions for Week of 08-01-2018</h1>
<p>You are bidding on the following item:</p>
<ul><li>This is the item for sale</li></ul>
<p>Condition is pack fresh unless otherwise indicated. Please review the pictures carefully and if you have any questions about something specific, ask.</p>
<p><b>Shipping:</b> Shipping will be calculated based on buyer location. No premiums are charged. Cards are mailed in an 8x5 bubble mailer and shipped First Class mail unless the price exceeds $50, at which point they will be shipped Priority at no additional cost to the buyer. If you win multiple auctions, please wait for an invoice to be sent.</p>
</div>
</body>"""

pprint(json.dumps({html_desc: "Some Value"}))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51460122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档