0
{
    "Sponge": {
        "orientation": "Straight",
        "gender": "Woman",
        "age": 23,
        "rel_status": "Single",
        "summary": "  Bonjour! Je m'appelle Jacqueline!, Enjoy cooking, reading and traveling!, Love animals, languages and nature :-)  ",
        "location": "Kao-hsiung-k’a",
        "id": "6693397339871"
    }
}

I have this json above and I'm trying to read it except there is some special character in it. For example the "’" in location. This raise some error when I'm trying to read the JSON:

UnicodeEncodeError: 'charmap' codec can't encode characters in position 27-28: character maps to <undefined>

I'm using python 3.5 and I have done the following code:

with open('test.json') as json_data:
    users = json.load(json_data)
print users
1
  • question title says UnicodeDecodeError. actual reported error is UnicodeEncodeError Commented Dec 30, 2016 at 21:12

2 Answers 2

1

Use codecs module to open the file for a quick fix.

with codecs.open('test.json', 'r', 'utf-8') as json_data:
    users = json.load(json_data)
    print(users)

Also answer to this question can be found easily on the web. (hint: that's how I learned about this module.)

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

2 Comments

Thank you for answer but I already test this and it didn't solve the problem. This caracter is not define with utf-8 " ’ " I still got: "UnicodeEncodeError: 'charmap' codec can't encode character '\u2019' in position 71: character maps to <undefined>"
If it's not UTF-8, have you tried figuring out what other encoding might work with your problem then?
0

Ok I find my solution it's a problem with the terminal of windows you have to type this in the terminal: chcp 65001

After that launch your program!

More explanation here: Why doesn't Python recognize my utf-8 encoded source file?

Comments

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.