[Python] Encode json file using with open

Discussão em 'Python' iniciado por Stack, Setembro 9, 2025.

  1. Stack

    Stack Membro Participativo

    I am using with open to open my json file and parse data from it, however some characters aren't ascii. I google around and found that you have to set encoding='utf8' in with open(), so I do this and get this error

    UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 21: ordinal not in range(128)


    So I figure I have to use ensure_ascii=False but then I get this error

    TypeError: 'ensure_ascii' is an invalid keyword argument for open()


    As you can see by the error ensure_ascii isn't compatible with open() but I don't know what is and I seem to be stuck and unable to find it.

    The code being used

    with open("languages.json", 'r', encoding='utf8') as f:
    languages = json.load(f)
    with open("config.json", 'r', encoding='utf8') as c:
    config = json.load(c)

    Continue reading...

Compartilhe esta Página