[Python] Azure functions decoding utf8 strings in Python

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm running this code on my Python code with version 3.10.6:

    def downloadRevistaXML():
    url= f"https://revistas.inpi.gov.br/txt/RM2823.zip"

    try:
    response = requests.get(url)
    response.raise_for_status()

    if response.headers.get("Content-Type") == "application/zip":
    with zipfile.ZipFile(io.BytesIO(response.content)) as z:
    for file_name in z.namelist():
    if file_name.endswith(".xml"):
    xml_file = z.open(file_name)
    xml_content = xml_file.read()


    xml_content = xml_content.decode("utf-8")

    #This doesn't print right
    print(xml_content)


    except requests.exceptions.RequestException as e:
    raise RuntimeError(f"Error during download: {e}")
    except zipfile.BadZipFile:
    raise ValueError("The file downloaded is not a valid ZIP archive.")


    I'm running this on Windows 10 with python 3.10.6.

    When I directly run the code, it prints the xml content with the correct string encoding utf-8. One node looks like this:

    <despacho codigo="IPAS009" nome="Publicação de pedido de registro para oposição (exame formal concluído)"/>


    But, if I run the same code LOCALLY using an azure function, it prints like this:

    <despacho codigo="IPAS009" nome="PublicaþÒo de pedido de registro para oposiþÒo (exame formal concluÝdo)"/>


    I was thinking this was some problem with my code encoding something in the wrong way, I tried every way of decoding this later but didn't find any solution. Then I tested without the azure function and it works perfectly.

    I'm using a http trigger Azure Function. Right now it's only hosting a server and when I call an endpoint it runs the function without any parameters.

    I've tried forcing other encodings like xml_content = xml_content.decode("UTF-8") but it doesn't change anything.

    I've also tried encoding and decoding again but not luck.

    Continue reading...

Compartilhe esta Página