0

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)
2
  • Which python version and json package you've used? Python 3 uses UTF-8 for opening by default and ensure_ascii is a param for json. Commented Mar 12, 2020 at 11:42
  • ensure_ascii is a param for json.dump that opposites of load, also not all json packages implement it. So it's not your case either. Try to do data = f.read(); json.loads(data). Commented Mar 12, 2020 at 11:49

1 Answer 1

1

I believe that utf8 is not the correct keyword, instead use utf-8 in your encoding=

Sign up to request clarification or add additional context in comments.

4 Comments

Could you show me any of the .json files? Also, don't use ensure_ascii; there's no need.
"deutsch": { "2": "Subject: Antrag auf Löschung personenbezogener Daten } is one of them
This line here works fine for me: with open("test.json", "r", encoding="UTF-8") as i: where test.json is {"deutsch": { "2": "Subject: Antrag auf Löschung personenbezogener Daten" }}
There is no difference in spelling between utf8, utf-8, and UTF-8 for Python.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.