mel
mel

Reputation: 2790

UnicodeDecodeError while decoding a json with python3.5

{
    "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

Upvotes: 0

Views: 299

Answers (2)

mel
mel

Reputation: 2790

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?

Upvotes: 0

spicypumpkin
spicypumpkin

Reputation: 1219

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.)

Upvotes: 1

Related Questions