Why Python is writting the UNICODE code instead the character on a file

230 Views Asked by At

I'm making a Python program that (after doing a lot of things haha) creates a HTML file with some of the generated info.

I open a HTML template and then I replace some 'tokens' with the generated info. The way I open and replace the info is the following:

def getPlantilla():
    with open('assets/plantillas/plantilla_proyecto3.html','r') as file:
        plantilla = file.read()
    return plantilla
    
def remplazarTokens(plantilla:str,PID,Pmap):
    tabla_html = tabulate(Pmap,headers="firstrow",tablefmt='html')
    return plantilla.format(PID=PID,TABLA=tabla_html)

But before 'replace the tokens' I generate some HTML code with the generated info with this function:

def crearTrigger(uso,id):
    return f"<a href=\"#{id}\">{uso}</a>"

And finally I create the file:

with open(filename,'w',encoding='UTF-8') as file:
        file.write(html)

The problem is that in the final .html, the code that was generated with crearTrigger() dosen't works well because some characters are remplaced with the UNICODE code.

Example:

Out: &lt;a href=&quot;#heap&quot;&gt;Heap&lt;/a&gt;

How it should be: <a href="#heap">Heap</a>

I think that this is a encoding problem, but I had tried to encode it with .encode("utf-8") and still have the same problem.

Hope someone can help me. Thanks

Update: When I was writting the question, I realised that the library tabulate that I using to convert the info into a HTML table, it's creating the problem (Putting the UNICODE code instead the char), because the out's from crearTrigger() are saving in a list, that later tabulate converts into a HTLM table. But I still dont know how to solve it.

0

There are 0 best solutions below