0

I am trying to print data from fetched JSON, however the data has unicode-decoded data. How can I encode (see example) it to display correctly. I am very new to python and was not able to get it working, I am using Windows 7, python 2.7 on a command line terminal. Thanks!

Example: results>>title:

'R\u00f6yksopp - 49 Percent' has to print it as 'Röyksopp - 49 Percent'

"title": "R\u00f6yksopp - 49 Percent",

JSON:

"results": [{
        "style": ["House", "Electro", "Synth-pop"],
        "thumb": "http://api.discogs.com/image/R-90-530519-1236701656.jpeg",
        "format": ["CD", "Maxi-Single"],
        "country": "Europe",
        "barcode": ["5 028589 023420", "BEL/BIEM", "LC 3098"],
        "uri": "/R%C3%B6yksopp-49-Percent/master/30161",
        "label": ["Virgin", "Labels", "Wall Of Sound"],
        "catno": "0946 3378752 0",
        "year": "2005",
        "genre": ["Electronic"],
        "title": "R\u00f6yksopp - 49 Percent",
        "resource_url": "http://api.discogs.com/masters/30161",
        "type": "master",
        "id": 30161
    }
6
  • \u00f6 is ö. Does Command Prompt support those characters? Commented Feb 1, 2013 at 20:39
  • Do you use a lib to parse json ? (do you "import json" or "import cjson" ?) Commented Feb 1, 2013 at 20:46
  • Show the actual Python code that you used, and what output you got. Commented Feb 1, 2013 at 22:59
  • #!/usr/bin/env python # -*- coding: utf-8 -*- import json d = json.loads(u'''{"title": "R\u00f6yksopp - 49 Percent"}''') print d['title'].encode('utf-8') Commented Feb 2, 2013 at 5:32
  • The above code doest not print correctly.. Commented Feb 2, 2013 at 5:32

1 Answer 1

1

Environment: Windows 7, default codepage = 850, Python 2.7.3

Using a cut-down version of your input:

>>> import json
>>> js = """{
...         "style": ["House", "Electro", "Synth-pop"],
...         "title": "R\u00f6yksopp - 49 Percent",
...         "id": 30161
...     }"""
>>>
>>> j = json.loads(js)
>>> j
{u'style': [u'House', u'Electro', u'Synth-pop'], u'id': 30161, u'title': u'R\xf6yksopp - 49 Percent'}
>>> j['title']
u'R\xf6yksopp - 49 Percent'
>>> print j['title']
Röyksopp - 49 Percent
>>>
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the response, but unfortunately my unicode string varies from request to request, for example, the code mentioned below wont output on my terminal, but the above code you posted works only for that symbol listed..I was looking at a generic solution to any unicode characters...<br><br>
#!/usr/bin/env python # -*- coding: utf-8 -*- import json js = """{"style": ["House", "Electro", "Synth-pop"],"title": "Gigi D\u2010Agostino - The Riddle","id": 30161}""" j = json.loads(js) print j['title'] - Here u\2010 is a hyphen ( I think) that wont display...I shall paste the terminal error I get below..
print j['title'] File "C:\Python27\lib\encodings\cp437.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\u2010' in position 1: character maps to <undefined>
There is no good generic solution for the Windows commandline. Try IDLE.

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.