I wrote a Python code to modify my html contents. But on writing that again to the html file, I get weird hexadecimal numbers
import re
search=\"www.abc.com\"
When you truncated your file, you didn't actually seek back to position 0 in the file. The file position is still where it was, which is now past the end of the file.
Writing to the file writes the text at the old file position, leaving everything before that point blank.
The hex isn't caused by anything in the code you've posted. You've opened the file in a tool that's showing you the raw hex values of the bytes. This tool might be doing that because of all the null bytes.