Odd json encoding erro

Chris Rebert clp2 at rebertia.com
Tue Dec 15 17:23:36 EST 2009


On Tue, Dec 15, 2009 at 2:03 PM, Wells <thewellsoliver at gmail.com> wrote:
> I get this exception when decoding a certain JSON string:
>
> 'ascii' codec can't encode character u'\u2019' in position 8: ordinal
> not in range(128)
>
> The JSON data in question:
>
> http://mlb.com/lookup/json/named.player_info.bam?sport_code=%27mlb%27&player_id=%27489002%27
>
> It's in the 'high_school' key. Is there some string function I can run
> on the information before I decode it to avoid this?

>From what I can guess (you didn't include any code), you're printing
the result of loading the JSON (which probably loaded correctly) to
the terminal without specifying the exact encoding to use. In such
cases, Python defaults to ASCII. However, your data obviously includes
non-ASCII characters, thus resulting in the error you're encountering.
Instead of `print the_high_school`, try `print
the_high_school.encode('utf8')`.

Note that the `json` library returns Unicode strings of the type
`unicode` and not byte strings of type `str` (unless you're using
Python 3.0, in which case `unicode` got renamed to `str` and `str` got
renamed to `bytes`). When outputting Unicode, it needs to be encoded
to bytes. The built-in type() function* can help determine when you
have Unicode data.

Cheers,
Chris
--
http://blog.rebertia.com

*Yes, it's not /really truly/ a function, but the distinction is not
relevant here.



More information about the Python-list mailing list