开发者

Unicode error on response from web service using python suds

I've seen the other threads about this issue, but I haven't seen an answer that helps me.

My issue is very similar to the person using "CJ's horrible web services" in a previous post.

I'm using python 2.5 and the suds library (version 0.4.1). I request some records from a database through a web service. I then try to print some of the fields of the returned records. Some of the titles of those records contain characters that cause an exception. The exception I get is:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u201d' in position 39: ordinal not in range(128)

My code looks like this: (sr is a Service Request, the type of record I'm retrieving from the DB)

response = client.service.QuerySRByExample(input_data)
for sr in response:
    print sr.SRNumber, sr.Title

If I loop through the offending开发者_JAVA百科 title using ord(), I can see that there are some double-quote characters that have code point 8220 and 8221. These are what is causing the error (The first double-quote is at position 39 of the title string, as per the error message.)

... 114 111 108 108 101 114 32 65 8221 32 43 32 8220 68 67 78 ...

If I instead use

    print sr.SRNumber, sr.Title.encode('ascii', 'ignore')

I don't get the error. It just drops the offending characters (anything with code point > 127).

Is there a better way to handle this? It seems like I should be able to convert the utf-8 double-quotes into ascii double-quotes somehow.

The web service says it is using utf-8 enoding. The first part of the response back from the web service is:

 <?xml version="1.0" encoding="UTF-8" ?> 
 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">

In the other thread, one user said he found something in the suds code and was able to fix it. I don't know if that was incorporated into the suds library.

Any help would be greatly appreciated.


It's just failing to print. If your terminal can handle utf-8 (Macs and most recent Linux), print sr.Title.encode("utf-8") should work. On Windows, I think you can try encoding with your system code page (probably cp1252) - but it might not have the necessary characters.

Upgrading to a newer version of Python might help. In 2.6 and 2.7, I can print out unicode characters without having to do anything special.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜