Using Python simplejson for transmitting JSON to another server results in unicode encoding problems
Posted
by
Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2011-01-08T23:01:04Z
Indexed on
2011/01/08
23:53 UTC
Read the original article
Hit count: 220
Hi there,
I'm encoding a string with Python's simplejson library with special characters:
hello testing
spécißl characters
plusses: +++++
special chars :œ?´®†¥¨ˆøp“ß?ƒ©??°¬O˜çv?˜µ==
However, when I encode it and transmit it to the other machine (using POST), it turns out like this:
{'message': ['{"body": "hello testing sp\\u00e9ci\\u00dfl characters\\n\\nplusses: \\n\\nspecial chars :\\u0153\\u2211\\u00b4\\u00ae\\u2020\\u00a5\\u00a8\\u02c6\\u00f8\\u03c0\\u201c\\u00df\\u2202\\u0192\\u00a9\\u02d9\\u2206\\u02da\\u00ac\\u03a9\\u2248\\u00e7\\u221a\\u222b\\u02dc\\u00b5\\u2264\\u2265"}']}
The + signs are completely stripped and the rest are in this unicode(?) format. My code for this is:
data = {'body': data_string}
data_encoded = json.dumps(data)
Any ideas? Thanks!
Edit: I've tried using json.dumps(data, ensure_ascii=False)
but it results in a UnicodeError ordinal not in range
error.
© Stack Overflow or respective owner