Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:17:48+00:00 2026-06-16T00:17:48+00:00

I have some problems with strange escaped unicode Strings. My script consumes a webservice

  • 0

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 \u003c, \u003c 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!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:17:48+00:00Added an answer on June 16, 2026 at 12:17 am

    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 ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some strange scrolling problems with my PhoneGap (using jQuery Mobile) app: I'm
I have some problems with Javascript. In fact, I'm just newbie in that script
Im using latest TCPDF version(5.9). But have some strange problems with encoding. I need
I am trying to use TextToSpeech but i have some strange problems. Let me
I'm new in JSF and I have some strange problems in displaying conditional parts
We are debugging some performace problems and have notice some strange results from stopwatch.
I have tried using the FileUpload control in ASP.net and found some strange problems.
Have this strange problems on some pages that rendering of umlauts (åäö) gets wrong
I having been running into some strange problems with a webpage that I have
I'm having some strange problems copying files in a custom script in TFS2008 without

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.