简单的unicode-safe版本的str(异常)? [英] Simple unicode-safe version of str(exception)?
问题描述
我有这样的代码:
除了Exception,e:
self.setState(self.Failed,str(e))
如果异常包含unicode参数则失败。
我当然尝试unicode(e)但是失败了。
以下工作,但似乎相当混乱:
除了异常,e:
errStr ="," .join([unicode(s)for s.f.args])
self.setState(self.Failed,errStr)
有没有更简单的解决方案适用于Python 2.3-2.5?
- Russell
I have code like this:
except Exception, e:
self.setState(self.Failed, str(e))
which fails if the exception contains a unicode argument.
I did, of course, try unicode(e) but that fails.
The following works, but seems rather messy:
except Exception, e:
errStr = ",".join([unicode(s) for s in f.args])
self.setState(self.Failed, errStr)
Is there a simpler solution that works in Python 2.3-2.5?
-- Russell
推荐答案
Russell E. Owen写道:
Russell E. Owen wrote:
我的代码如下:
除了Exception,e:
self.setState(self.Failed,str(e))
失败。
I have code like this:
except Exception, e:
self.setState(self.Failed, str(e))
which fails if the exception contains a unicode argument.
如何失败?
Fails how?
我当然尝试了unicode(e)但是失败了。
I did, of course, try unicode(e) but that fails.
将unicode转换为unicode无济于事。而不仅仅是e,尝试使用e.encode(some-encoding)来使用
,其中某些编码应该是你的
系统的默认编码,如utf- 8或iso8859-1。
问候,
Bj?
-
BOFH借口#434:
Converting unicode to unicode doesn''t help. Instead of just e, try
using e.encode("some-encoding") where some-encoding should be your
system''s default encoding like utf-8 or iso8859-1.
Regards,
Bj?rn
--
BOFH excuse #434:
Please state the nature of the technical emergency
> I有这样的代码:
>I have code like this:
>除了异常,e:
self.setState(self.Failed,str( e))如果异常包含unicode参数,则失败。
>except Exception, e:
self.setState(self.Failed, str(e))
which fails if the exception contains a unicode argument.
失败怎么办?
Fails how?
我想是ASCII编码错误。只有当a)一个参数
是一个Unicode对象,并且b)Unicode对象包含非ASCII
参数时,它才会失败。
ASCII encoding error, I suppose. It fails only if a) one argument
is a Unicode object, and b) that Unicode object contains non-ASCII
parameters.
>我当然尝试了unicode(e),但失败了。
>I did, of course, try unicode(e) but that fails.
将unicode转换为unicode并没有帮助。
Converting unicode to unicode doesn''t help.
e是一个异常对象,而不是Unicode对象。
问候,
Martin
e is an exception object, not a Unicode object.
Regards,
Martin
文章< 48 ************** @ v.loewis.de>,
Martin v.L?wis < ma **** @ v.loewis.dewrote:
In article <48**************@v.loewis.de>,
"Martin v. L?wis" <ma****@v.loewis.dewrote:
我有类似的代码这个:
除了异常,e:
self.setState(self.Failed,str(e))
如果异常包含失败一个unicode参数。
I have code like this:
except Exception, e:
self.setState(self.Failed, str(e))
which fails if the exception contains a unicode argument.
失败怎么办?
Fails how?
我想是ASCII编码错误。只有当a)一个参数
是一个Unicode对象,并且b)该Unicode对象包含非ASCII
参数时,它才会失败。
ASCII encoding error, I suppose. It fails only if a) one argument
is a Unicode object, and b) that Unicode object contains non-ASCII
parameters.
看来讽刺的是,即使几乎任何东西都失败了
else是str()的有效输入 - socket,dict,等等?
一种通用的解决方案可能是遵循str的行为
关于''_ _ _ _ _ _ _ _ _ _ _''''''''''''''''''''''''''''''''''''''''' repr()
无论出现什么问题。
def xtr(a):
试试:
return str( a)
除外:
返回repr(a)
...
self。 setState(self.Failed,xtr(e))
Donn Cave,做** @ u。 washington.edu
这篇关于简单的unicode-safe版本的str(异常)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!