I have my sample.htm file as follows:
<html><head>
<title>hello</title>
</head>
<body>
<p> Hello! he said. !</p>
</body>
</html>
I have my python code as follows:
with open('sample.htm', 'r',encoding='utf8') as f:
contents = f.read()
soup = BeautifulSoup(contents, 'html.parser')
with open("sample-output.htm", "w", encoding='utf-8') as file:
file.write(str(soup))
This reads the sample.htm and writes to another sample-output.htm
The output of the above:
<html><head>
<title>hello</title>
</head>
<body>
<p> Hello! he said. !</p>
</body>
</html>
How can i preserve the after writing to the file.