0

So I have a HTML file when I paste it in Python code in Multi_line String it gives me an KeyError '\n' while using KWargs**.

So the html file is like this:

<style>
body{
some-css:10px;
}
</style>
<body>
<h1>{param1}</h1>
<h2>{param2}</h2>
</body>

Python Code where it gives error:

html="""<style>
    body{
    some-css:10px;
    }
    </style>
    <body>
    <h1>{param1}</h1>
    <h2>{param2}</h2>
    </body>"""
print(html.format(param1=12,param2=22))
Mahesh Jaganiya
  • 155
  • 1
  • 2
  • 11
  • The curly braces in the CSS are tripping up `format`. – Aran-Fey Jun 13 '18 at 06:54
  • You need to escape the `{` and `}` because the format is trying to substitute the text enclosed within them. – Mike Robins Jun 13 '18 at 06:54
  • @MikeRobins Ok I searched it can be escaped with {{ }} , how can i convert all the occurences in {{ }} without going for it manually. – Mahesh Jaganiya Jun 13 '18 at 07:00
  • You need to determine what heuristics you can use to identify the two uses of the braces. Then use text substitution `string.replace` or `re.sub` to escape the appropriate ones. e.g. is `body {} ` the only occurrence or are there other styles. or split the ` – Mike Robins Jun 13 '18 at 08:59

0 Answers0