如何在Python 3.6中解决UnicodeDecodeError? [英] How to solve UnicodeDecodeError in Python 3.6?

查看:226
本文介绍了如何在Python 3.6中解决UnicodeDecodeError?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Python 2.7切换到Python 3.6.

I am switched from Python 2.7 to Python 3.6.

我有处理某些非英语内容的脚本.

I have scripts that deal with some non-English content.

我通常通过Cron以及在Terminal中运行脚本.

I usually run scripts via Cron and also in Terminal.

我的Python 2.7脚本中出现了UnicodeDecodeError,我解决了这个问题.

I had UnicodeDecodeError in my Python 2.7 scripts and I solved by this.

# encoding=utf8  
import sys  

reload(sys)  
sys.setdefaultencoding('utf8')

现在在Python 3.6中,它不起作用.我有类似print("Here %s" % (myvar))的打印语句,它会引发错误.我可以通过将其替换为myvar.encode("utf-8")来解决此问题,但我不想用每个print语句编写.

Now in Python 3.6, it doesnt work. I have print statements like print("Here %s" % (myvar)) and it throws error. I can solve this issue by replacing it to myvar.encode("utf-8") but I don't want to write with each print statement.

我在终端中做了PYTHONIOENCODING=utf-8,但仍然存在该问题.

I did PYTHONIOENCODING=utf-8 in my terminal and I have still that issue.

在Python 3.6中,有没有更干净的方法来解决UnicodeDecodeError问题?

Is there a cleaner way to solve UnicodeDecodeError issue in Python 3.6?

有什么方法可以告诉Python3在utf-8中打印所有内容吗?就像我在Python2中所做的一样?

is there any way to tell Python3 to print everything in utf-8? just like I did in Python2?

推荐答案

听起来您的语言环境已损坏并出现了另一个字节-> Unicode问题.您为Python 2.7做的事情是只掩盖了真正的问题的黑客(这是您必须reload sys使其起作用的原因).

It sounds like your locale is broken and have another bytes->Unicode issue. The thing you did for Python 2.7 is a hack that only masked the real problem (there's a reason why you have to reload sys to make it work).

要修复您的语言环境,请尝试从命令行键入locale.它看起来应该像这样:

To fix your locale, try typing locale from the command line. It should look something like:

LANG=en_GB.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_GB.UTF-8"
LC_TIME="en_GB.UTF-8"
LC_COLLATE="en_GB.UTF-8"
LC_MONETARY="en_GB.UTF-8"
LC_MESSAGES="en_GB.UTF-8"
LC_ALL=

locale取决于正确设置的LANG. Python有效地使用locale来算出向stdout写入时使用的编码.如果无法算出,则默认为ASCII.

locale depends on LANG being set properly. Python effectively uses locale to work out what encoding to use when writing to stdout in. If it can't work it out, it defaults to ASCII.

您应该首先尝试修复您的语言环境.如果出现locale错误,请确保已为您所在的地区安装了正确的语言包.

You should first attempt to fix your locale. If locale errors, make sure you've installed the correct language pack for your region.

如果其他所有方法均失败,则始终可以通过设置PYTHONIOENCODING=UTF-8来修复Python.这将被用作最后的手段,因为您将再次掩盖问题.

If all else fails, you can always fix Python by setting PYTHONIOENCODING=UTF-8. This should be used as a last resort as you'll be masking problems once again.

如果设置PYTHONIOENCODING后Python仍然引发错误,请使用stacktrace更新您的问题.您有可能正在进行隐式转换.

If Python is still throwing an error after setting PYTHONIOENCODING then please update your question with the stacktrace. Chances are you've got an implied conversion going on.

这篇关于如何在Python 3.6中解决UnicodeDecodeError?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆