Windows 10中Google Cloud项目的python 2.7 Unicode编码错误 &



我在Python中有一个API,如果它在Mac上运行没有错误,只发生在Windows 10上,我使用Python 2.7。我不想修改它,因为该项目在PROD和mac本地运行良好,但我想让它在我的Windows机器上工作。该错误来自其他库而不是我的代码,该项目除Windows外工作良好。

E:Program Files (x86)GoogleCloud SDKgoogle-cloud-sdklibthird_partypkg_resources__init__.py:2689: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal   if item == nloc: E:Program Files (x86)GoogleCloud SDKgoogle-cloud-sdklibthird_partypkg_resources__init__.py:2695: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal   elif item
== bdir and self.precedence == EGG_DIST: Traceback (most recent call last):   File "E:Program Files (x86)GoogleCloud SDKgoogle-cloud-sdkbindev_appserver.py", line 14, in <module>
from bootstrapping import bootstrapping   File "E:Program Files (x86)GoogleCloud SDKgoogle-cloud-sdkbinbootstrappingbootstrapping.py", line 39, in <module>
from googlecloudsdk.core import config   File "E:Program Files (x86)GoogleCloud SDKgoogle-cloud-sdklibgooglecloudsdkcoreconfig.py", line 33, in <module>
from oauth2client import client   File "E:Program Files (x86)GoogleCloud SDKgoogle-cloud-sdklibthird_partyoauth2clientclient.py", line 52, in <module>
from oauth2client import crypt   File "E:Program Files (x86)GoogleCloud SDKgoogle-cloud-sdklibthird_partyoauth2clientcrypt.py", line 56, in <module>
from oauth2client import _pycrypto_crypt   File "E:Program Files (x86)GoogleCloud SDKgoogle-cloud-sdklibthird_partyoauth2client_pycrypto_crypt.py", line 17, in <module>
from Crypto.PublicKey import RSA   File "e:tempeasy_install-38xi7lpycrypto-2.6.1-py2.7-win-amd64.egg.tmpCryptoPublicKeyRSA.py", line 78, in <module>   File "e:tempeasy_install-38xi7lpycrypto-2.6.1-py2.7-win-amd64.egg.tmpCryptoRandom__init__.py", line 28, in <module>   File "e:tempeasy_install-38xi7lpycrypto-2.6.1-py2.7-win-amd64.egg.tmpCryptoRandomOSRNG__init__.py", line 34, in <module>   File "e:tempeasy_install-38xi7lpycrypto-2.6.1-py2.7-win-amd64.egg.tmpCryptoRandomOSRNGnt.py", line 28, in <module>   File "e:tempeasy_install-38xi7lpycrypto-2.6.1-py2.7-win-amd64.egg.tmpCryptoRandomOSRNGwinrandom.py", line 7, in <module>   File "e:tempeasy_install-38xi7lpycrypto-2.6.1-py2.7-win-amd64.egg.tmpCryptoRandomOSRNGwinrandom.py", line 6, in __bootstrap__ UnicodeEncodeError: 'ascii' codec can't encode character u'xe9' in position 13: ordinal not in range(128)

在检查是否相等时,您正在混合Unicode字符串和字节字符串,您可以通过打印type(comparable 1)type(comparable 2)来确认,其中可比1,可比2是您正在比较的。此错误发生在Python 2中。X在比较Unicode字符串和字节字符串时。Python 2。X尝试使用默认的ascii编解码器隐式地将字节字符串转换为Unicode字符串。如果由于字节字符串包含非ascii字节而失败,则出现该警告。

有三种方法可以尝试解决这个问题:

  1. 尽早解码文本数据,只比较Unicode对象。因此首先解码为UTF-8将解决警告。

在Windows上可以通过以下命令设置UTF-8:

set PYTHONIOENCODING=UTF-8
Or, create an environment variable with the name 'PYTHONIOENCODING' and value 'UTF-8'
Or, $env:PYTHONIOENCODING='UTF-8' with Powershell

  1. 按照John Hanley的建议将代码转换为Python 3。Python3.X通过不允许在Unicode字符串字面值中使用非ascii字符来减少混淆。

  2. 您还可以将非ASCII字符更改为ASCII字符,Python 2。X在尝试转换字节时不会失败使用ascii编解码器将字符串转换为Unicode。