我可以关闭隐含的Python

我可以关闭隐含的Python

本文介绍了我可以关闭隐含的Python unicode转换来查找我的混合字符串错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

在剖析我们的代码时,我惊奇地发现数百万次调用到

C:\Python26\lib\encodings\utf_8.py:15(decode)

When profiling our code I was surprised to find millions of calls to
C:\Python26\lib\encodings\utf_8.py:15(decode)

我开始调试,发现在我们的代码库中有许多小错误,通常将字符串与unicode进行比较,或添加sting和unicode。 Python慷慨解码字符串,并在unicode中执行以下操作。

I started debugging and found that across our code base there are many small bugs, usually comparing a string to a unicode or adding a sting and a unicode. Python graciously decodes the strings and performs the following operations in unicode.

怎么样但是昂贵!

我流利的unicode,读了和 ...

I am fluent in unicode, having read Joel Spolsky and Dive Into Python...

我尝试保留我们的代码内部文件为unicode。

I try to keep our code internals in unicode only.

我的问题 - 我可以关闭这个pythonic好人的行为吗?至少直到我找到所有这些错误并修复它们(通常是添加一个u'u')?

My question - can I turn off this pythonic nice-guy behavior? At least until I find all these bugs and fix them (usually by adding a u'u')?

其中一些非常难找到(有时是字符串的变量...)。

Some of them are extremely hard to find (a variable that is sometimes a string...).

Python 2.6.5(我不能切换到3.x)

Python 2.6.5 (and I can't switch to 3.x).

推荐答案

以下内容应该可以工作: p>

The following should work:

>>> import sys
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('undefined')
>>> u"abc" + u"xyz"
u'abcxyz'
>>> u"abc" + "xyz"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/encodings/undefined.py", line 22, in decode
    raise UnicodeError("undefined encoding")
UnicodeError: undefined encoding

reload(sys)上面的代码段中的code>仅在这里需要,因为通常 sys.setdefaultencoding 应该在 sitecustomize.py 文件在您的Python site-packages 目录(建议这样做)。

reload(sys) in the snippet above is only necessary here since normally sys.setdefaultencoding is supposed to go in a sitecustomize.py file in your Python site-packages directory (it's advisable to do that).

这篇关于我可以关闭隐含的Python unicode转换来查找我的混合字符串错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 17:23