Quantcast
Channel: Active questions tagged utf-8 - Stack Overflow
Viewing all articles
Browse latest Browse all 1145

Python json.dumps of a tuple with some UTF-8 characters, either fails or converts them. I want the encoded character retained as is

$
0
0

On my server, a Python script gets data from a database as a tuple. Then the script converts the tuple to a string (using json.dumps()) to be passed to the JavaScript script in the user's browser.

The data include German names such as Weidmüller. When the Python scrip gets that data, it returns it as Weidm\xfcller, where \xfc is the UTF-8 encoding of ü. So far so good.

However,

  • json.dumps(tableData,ensure_ascii=False) converts the \xfc to �
  • json.dumps(tableData,ensure_ascii=True) fails: "UnicodeDecodeError: 'utf8' codec can't decode byte 0xfc in position 5: invalid start byte"

What I really want is for json.dumps to leave the UTF-8 encoded character alone; to just pass the \xfc as is. That way the JavaScript script in the user's browser can do the decoding.Is that possible?

Or, am I approaching the problem incorrectly?

EDIT: Yes, I was approaching the problem incorrectly. \xfc is latin encoding, not UTF-8.

Solution:

json.dumps(tableData,encoding='latin1',ensure_ascii=True)


Viewing all articles
Browse latest Browse all 1145

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>