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
mel
  • 2,730
  • 8
  • 35
  • 70

2 Answers2

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

spicypumpkin
  • 1,209
  • 2
  • 10
  • 21
  • 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 " – mel Dec 21 '16 at 07:23
  • If it's not UTF-8, have you tried figuring out what other encoding might work with your problem then? – spicypumpkin Dec 21 '16 at 07:38
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?

Community
  • 1
  • 1
mel
  • 2,730
  • 8
  • 35
  • 70