简体   繁体   中英

python mixed single and double unicode escape sequences

I have some problems with strange escaped unicode Strings. My script consumes a webservice via the request library and response.text contains the following unicode string:

 u'\\u003c? abc ?\\u003eDas Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von \xd6kosystemen abgeleitet.\\u003c? /abc ?\\u003e'

 **Updated** Martijn solution works with the upper one, but breaks with this one because of len="12"
 u'\\u003c?abc len="12"?\\u003eResilienz sollte als st\xe4ndiger Anpassungsprozess zwischen Systemen und der Umwelt begriffen werden.\\u003c? /abc ?\\u003e'

The response from the server looks something like this:

\u003c? abc ?\u003eDas Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von Ökosystemen abgeleitet.\u003c?dpf /sent ?\u003e

The problem are the double escaped unicode sequences like \<, \< normally represents a < char. \\xd6 is correct and represents a german Ö. This double escaping totally messes up my unicode string :-)

I have found a similar problem at this post: Stack Overflow - Conversion of strings like \\uXXXX in python

The solution, using string.decode('unicode-escape'), only seems to work if all unicode sequences would be escaped but not with mixed single and double escapes. Just replacing the double escapes with single ones gives me a corrupt unicode string.

The easiest and best solution would be to adjust the response encoding on the server side, but i have no access ...

Thank's for your help!!!

I suspect the server is returning JSON strings. JSON uses the same escape sequence, and if you add quotes around the string json.loads() is perfectly happy to decode that example for you:

>>> txt = u'\\u003c? abc ?\\u003eDas Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von \xd6kosystemen abgeleitet.\\u003c? /abc ?\\u003e'
>>> content = txt.encode('utf8')
>>> content
'\\u003c? abc ?\\u003eDas Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von \xc3\x96kosystemen abgeleitet.\\u003c? /abc ?\\u003e'
>>> import json
>>> json.loads('"{0}"'.format(content))
u'<? abc ?>Das Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von \xd6kosystemen abgeleitet.<? /abc ?>'
>>> print json.loads('"{0}"'.format(content))
<? abc ?>Das Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von Ökosystemen abgeleitet.<? /abc ?>

Try using json.loads('"{0}"'.format(response.content)) to decode the response to Unicode.

Your updated version does contain quotes, a little vexing, since those would have to be escaped to be using in valid JSON. It probably is not JSON then, but some other form of escapes; Java and Ruby also use \\uxxxx escapes. Next thing we can try is to use a regular expression to replace these:

import re

uescapes = re.compile(r'(?<!\\)\\u[0-9a-fA-F]{4}', re.UNICODE)
def uescape_decode(match): return match.group().decode('unicode_escape')

uescapes.sub(uescape_decode, response.text)

This regular expression will decode any \\uxxxx match to it's unicode character equivalent, provided that it is not preceded by a \\ , which effectively escapes the escape; \\\\uxxxx is not going to be replaced.

The regular expression approach decodes your both examples (second decoded first):

>>> print uescapes.sub(uescape_decode, txt)
<?abc len="12"?>Resilienz sollte als ständiger Anpassungsprozess zwischen Systemen und der Umwelt begriffen werden.<? /abc ?>
>>> print uescapes.sub(uescape_decode, u'\\u003c? abc ?\\u003eDas Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von \xd6kosystemen abgeleitet.\\u003c? /abc ?\\u003e')
<? abc ?>Das Modell des Adaptiven Zyklus wurde aus vergleichenden Untersuchungen zur Dynamik von Ökosystemen abgeleitet.<? /abc ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM