我应该在Python3中使用编码声明吗? [英] Should I use encoding declaration in Python3?

查看:382
本文介绍了我应该在Python3中使用编码声明吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试切换python3。我相信默认情况下是utf8编码。我还应该在每个文件的开始处使用编码声明?

 # -  *  - 编码:utf-8  -  *  -  


解决方案

因为默认的 8,您只需在偏离默认值时使用该声明,或者您依赖其他工具(如IDE或文本编辑器)来使用该信息。



换句话说,就Python而言,只有当你想使用不同的编码,你必须使用该声明。



请注意它只适用于Python如何读取源代码。它不适用于打印,打开文件或任何其他I / O操作。有关Python,Unicode和编码的更多详细信息,我强烈要求您阅读 Python Unicode HOWTO 或非常全面的 务实Unicode 谈话由Ned Batchelder。


I'm trying to switch python3. I believe there is utf8 encoding by default. Should I still use encoding declaration at the beginning every file?

# -*- coding: utf-8 -*-

解决方案

Because the default is UTF-8, you only need to use that declaration when you deviate from the default, or if you rely on other tools (like your IDE or text editor) to make use of that information.

In other words, as far as Python is concerned, only when you want to use an encoding that differs do you have to use that declaration.

Note that it only applies to how Python reads the source code. It doesn't apply to printing, opening files, or any other I/O operations. For more details on Python, Unicode, and encodings, I strongly urge you to read the Python Unicode HOWTO, or the very thorough Pragmatic Unicode talk by Ned Batchelder.

这篇关于我应该在Python3中使用编码声明吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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