简体   繁体   English

使用python suds从Web服务响应时出现Unicode错误

[英]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. 我的问题与上一篇文章中使用“ CJ的可怕的Web服务”的人非常相似。

I'm using python 2.5 and the suds library (version 0.4.1). 我正在使用python 2.5和suds库(版本0.4.1)。 I request some records from a database through a web service. 我通过Web服务从数据库中请求一些记录。 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) 我的代码如下所示:(sr是一个服务请求,我从数据库中检索的记录类型)

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

If I loop through the offending 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.) 如果我使用ord()遍历有问题的标题,则可以看到有些双引号字符的代码点为8220和8221。这是导致错误的原因(第一个双引号位于字符的第39位置)标题字符串,根据错误消息。)

... 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). 它只是删除有问题的字符(任何代码点大于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. 看来我应该能够以某种方式将utf-8双引号转换为ascii双引号。

The web service says it is using utf-8 enoding. Web服务说它正在使用utf-8 enoding。 The first part of the response back from the web service is: 从Web服务返回的响应的第一部分是:

 <?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. 在另一个线程中,一个用户说他在suds代码中找到了一些东西并能够修复它。 I don't know if that was incorporated into the suds library. 我不知道这是否已合并到suds库中。

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. 如果您的终端可以处理utf-8(Mac和最新的Linux),请print sr.Title.encode("utf-8") On Windows, I think you can try encoding with your system code page (probably cp1252) - but it might not have the necessary characters. 在Windows上,我认为您可以尝试使用系统代码页(可能是cp1252)进行编码-但它可能没有必要的字符。

Upgrading to a newer version of Python might help. 升级到较新版本的Python可能会有所帮助。 In 2.6 and 2.7, I can print out unicode characters without having to do anything special. 在2.6和2.7中,我可以打印出unicode字符,而无需执行任何特殊操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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