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 7003225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:02:22+00:00 2026-05-27T21:02:22+00:00

Here’s a common error when dealing with UTF-8 – ‘invalid tokens’ In my example,

  • 0

Here’s a common error when dealing with UTF-8 – ‘invalid tokens’

In my example, It comes from dealing with a SOAP service provider that had no respect for unicode characters, simply truncating values to 100 bytes and neglecting that the 100’th byte may be in the middle of a multi-byte character: for example:

<name xsi:type="xsd:string">浙江家庭教会五十人遭驱散及抓打 圣诞节聚会被断电及抢走物品(图、视频\xef\xbc</name>

The last two bytes are what remains of a 3 byte unicode character, after the truncation knife assumed that the world uses 1-byte characters. Next stop, sax parser and:

xml.sax._exceptions.SAXParseException: <unknown>:1:2392: not well-formed (invalid token)

I don’t care about this character anymore. It should be removed from the document and allow the sax parser to function.

The XML reply is valid in every other respect except for these values.

Question: How do you remove this character without parsing the entire document and re-inventing UTF-8 encoding to check every byte?

Using: Python+SUDS

  • 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-05-27T21:02:23+00:00Added an answer on May 27, 2026 at 9:02 pm

    Turns out, SUDS sees xml as type ‘string’ (not unicode) so these are encoded values.

    1) The FILTER:

    badXML = "your bad utf-8 xml here"  #(type <str>)
    
    #Turn it into a python unicode string - ignore errors, kick out bad unicode
    decoded = badXML.decode('utf-8', errors='ignore')  #(type <unicode>)
    
    #turn it back into a string, using utf-8 encoding.
    goodXML = decoded.encode('utf-8')   #(type <str>)
    

    2) SUDS: see https://fedorahosted.org/suds/wiki/Documentation#MessagePlugin

    from suds.plugin import MessagePlugin
    class UnicodeFilter(MessagePlugin):
        def received(self, context):
            decoded = context.reply.decode('utf-8', errors='ignore')
            reencoded = decoded.encode('utf-8')
            context.reply = reencoded
    

    and

    from suds.client import Client
    client = Client(WSDL_url, plugins=[UnicodeFilter()])
    

    Hope this helps someone.


    Note: Thanks to John Machin!

    See: Why is python decode replacing more than the invalid bytes from an encoded string?

    Python issue8271 regarding errors='ignore' can get in your way here. Without this bug fixed in python, ‘ignore’ will consume the next few bytes to satisfy the length

    during the decoding of an invalid UTF-8 byte sequence, only the
    start byte and the continuation byte(s) are now considered invalid,
    instead of the number of bytes specified by the start byte

    Issue was fixed in:

    Python 2.6.6 rc1
    Python 2.7.1 rc1 (and all future releases of 2.7)
    Python 3.1.3 rc1 (and all future release of 3.x)

    Python 2.5 and below will contain this issue.

    In the example above, "\xef\xbc</name".decode('utf-8', errors='ignore') should
    return "</name", but in ‘bugged’ versions of python it returns "/name".

    The first four bits (0xe) describes a 3-byte UTF character, so the bytes0xef, 0xbc, and then (erroneously) 0x3c ('<') are consumed.

    0x3c is not a valid continuation byte which creates the invalid 3-byte UTF character in the first place.

    Fixed versions of python only remove the first byte and only valid continuation bytes, leaving 0x3c unconsumed

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my persistence.xml : <?xml version=1.0 encoding=UTF-8?> <persistence xmlns=http://java.sun.com/xml/ns/persistence xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd version=1.0>
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here is my query: SELECT * FROM [GeoName] WHERE ((-26.3665122100029-Lat)*(-26.3665122100029-Lat))+((27.5978928658078-Long)*(27.5978928658078-Long)) < 0.005 ORDER BY
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM
Here an example of my checkbox list http://jsfiddle.net/YnM2f/ Let's say I check on G
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Here is an example: I write html code inside of textarea, then I swap
Here's my Manifest: <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.mappp.mobile android:versionCode=1 android:versionName=1.0 > <supports-screens android:largeScreens=true
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here is an example: I have the generic type called Account. I wish to

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.