xgqfrms / Python

Python27 & Python35
https://python.xgqfrms.xyz
2 stars 2 forks source link

Python & UTF-8 #16

Open xgqfrms opened 1 year ago

xgqfrms commented 1 year ago

Python & UTF-8

Python 3.x

# encoding=utf-8

Python 2.x

# -*- coding: UTF-8 -*-

image

如何在Python 中使用UTF-8 编码 && Python 使用 注释,Python ,UTF-8 编码 , Python 注释

作者:xgqfrms 链接:https://www.cnblogs.com/xgqfrms/p/5745802.html 来源:https://www.cnblogs.com 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 ©xgqfrms 2012-2022 www.cnblogs.com 发布文章使用:只允许注册用户才可以访问! 原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!

xgqfrms commented 1 year ago

https://www.runoob.com/python3/python3-tutorial.html

https://www.runoob.com/python/python-tutorial.html

Python 中文编码 / UTF-8 编码

Python中默认的编码格式是 ASCII 格式,在没修改编码格式时无法正确打印汉字,所以在读取中文时会报错。

解决方法为只要在文件开头加入 # -*- coding: UTF-8 -*- 或者 # coding=utf-8 就行了

注意:# coding=utf-8 的 = 号两边不要空格。

https://www.runoob.com/python/python-chinese-encoding.html

https://peps.python.org/pep-0263/

xgqfrms commented 1 year ago

Use # coding: utf8 instead of # -*- coding: utf-8 -*- which is far easier to remember. –

show0k Apr 10, 2017 at 13:35

xgqfrms commented 1 year ago

Python UTF-8 encode

Python 3.x

#!/usr/bin/python3

# coding=utf-8
#!/usr/bin/python3

# coding: utf-8

Python 2.x

#!/usr/bin/python2

# -*- coding: utf-8 -*-
#!/usr/bin/python2

# -*- coding: UTF-8 -*-
xgqfrms commented 1 year ago

https://www.imooc.com/qadetail/127992

我用的是PyCharm,在PyCharm里可以这么设置

进入File->settings->Editor->File and Code Templates->Python Script

输入一下内容:

1 #coding: utf-8
image image