I use library "suds" to communicate with SOAP server. After request was successfully done, I receive answer:
answer = client.invoke('RetrieveBLABLAObject', modelthings)
This answer is a structure of many different fields of requested object. Each field is structure that consist of "string" parameter "_type" and parameter "value", that may have different types.
answer[key][value] - returns me value of parameter "value". But in debug mode (I use Python 2.7.6 and PyCharm) it shows me that value has type "Text". Not "str", not "unicode", but "Text".
If I check it by
isinstance(obj[cur_key]['value'], unicode)
it shows me that it counts this text as "unicode". But if I compare it with unicode with same body, it returns false:
if obj[cur_key]['value'] != u'String that I know is there':
print("true") #it is printing, but it shouldn't
Why this happens? How can I convert "Text" to unicode? I tried
obj[cur_key]['value'].decode('utf-8')
it falls with exception. How can I handle this "Text" type?
You are looking at a subclass of unicode most likely. I think you have an instance of suds.sax.text.Text() here. Since this is a subclass, isinstance(obj, unicode) is True.
It's value is already a unicode string, so decoding will trigger an implicit encode first with ASCII, which will indeed fail.
You can convert the type to unicode by simply using:
unicode(obj[cur_key]['value'])
but take into account the value can still be XML escaped; the .unescape() method returns an unescaped version (it'll return self if the value wasn't escaped to begin with).
When comparing with another string, make sure they are exactly equal. Unicode values can contain many 'hidden' values, like zero-width characters or combining characters that can be represented in combined form as well. Use repr(value) to get an escaped representation that makes such codepoints more obvious.
The suds.sax.text.Text() class doesn't special-case equality testing, so your != test would work just the same as if the value was a unicode string instead of the subclass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With