2

I am having a really hard time getting the "marker" to show up. I do not know how to use .format() correctly to show the marker inside the string.

Does the variable need to be in a specific location in the string? Trying to get a grasp of this for the first time. Sorry if I am asking basic questions.

Keep getting : """.format(marker) KeyError: 'font-family'. Not sure where the problem is.

marker = "AUniqueMarker"    


# Create the body of the message (a plain-text and an HTML version).
text = "This is a test message.\nText and html."
html = """\
<html xmlns:v="urn:schemas-microsoft-com:vml"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:w="urn:schemas-microsoft-com:office:word"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml"
xmlns="http://www.w3.org/TR/REC-html40">

<head>
<meta http-equiv=Content-Type content="text/html; charset=windows-1252">
<meta name=ProgId content=Word.Document>
<meta name=Generator content="Microsoft Word 15">
<meta name=Originator content="Microsoft Word 15">
<link rel=File-List href="Law_files/filelist.xml">
<!--[if gte mso 9]><xml>

# (...)

-->
</style>
<!--[if gte mso 10]>
<style>
 /* Style Definitions */
 table.MsoNormalTable
    {mso-style-name:"Table Normal";
    mso-tstyle-rowband-size:0;
    mso-tstyle-colband-size:0;
    mso-style-noshow:yes;
    mso-style-priority:99;
    mso-style-parent:"";
    mso-padding-alt:0in 5.4pt 0in 5.4pt;
    mso-para-margin:0in;
    mso-para-margin-bottom:.0001pt;
    mso-pagination:widow-orphan;
    font-size:10.0pt;
    font-family:"Calibri","sans-serif";
    mso-ascii-font-family:Calibri;
    mso-ascii-theme-font:minor-latin;
    mso-hansi-font-family:Calibri;
    mso-hansi-theme-font:minor-latin;}
</style>
<![endif]--><!--[if gte mso 9]><xml>
 <o:shapedefaults v:ext="edit" spidmax="1026"/>
</xml><![endif]--><!--[if gte mso 9]><xml>
 <o:shapelayout v:ext="edit">
  <o:idmap v:ext="edit" data="1"/>
 </o:shapelayout></xml><![endif]-->
</head>

<body lang=EN-US style='tab-interval:.5in'>

{marker}
</body>

</html> 
 """.format(marker=marker)
1
  • From the documentation: If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}. Commented Oct 20, 2014 at 13:51

3 Answers 3

5

You need to escape the presence of other curly braces ({ and }) in the string, otherwise the string will be misinterpreted by format.

You have to repeat the characters to escape them. In the string you are calling format on, change the line

{mso-style-name:"Table Normal";

to

{{mso-style-name:"Table Normal";

and similarly for the closing bracket.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. It did the trick. Had to escape all braces in the HTML. Thank you for the speedy response. Thought it was going to take a week to get anything back!
1

You must double the { and } in your string, otherwise format will try to interpret text between braces.

You can use replace to do that :
html = """\
your html code
""".replace("{", "{{").replace("}", "}}").format(marker=marker)

EDIT : replace will transform {marker} into {{marker}}, so it will not be interpreted by format...

1 Comment

Thank you, the main idea was there and it helped solve the problem :)
0

If you don't want to manipulate the HTML code and add extra {} or %, python's Template string might be exactly what you’re looking for. Let’s take a look at an example:

from string import Template

t = Template('<body> $marker </body>')
t.substitute(marker='Hello World!')
'<body> Hello World! </body>'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.