前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python默认编码设置

python默认编码设置

作者头像
李拜六不开鑫
发布2018-09-04 13:34:00
2.8K0
发布2018-09-04 13:34:00
举报
文章被收录于专栏:本立2道生本立2道生

打开python 的gui,输入

代码语言:javascript
复制
import sys
sys.getdefaultencoding()

查询系统当前默认编码

默认情况下显示编码方式为ASCII

在python安装目录下的Lib/site-packages文件夹下新建sitecustomize.py,内容为

代码语言:javascript
复制
# -*- coding: UTF-8 -*- 
import sys  
reload(sys)   
sys.setdefaultencoding('utf8')

重启python,再次检查编码方式,发现已经改为utf8  

sitecustomize.py is a special script; Python will try to import it on startup, so any code in it will be run automatically. As the comment mentions, it can go anywhere (as long as import can find it), but it usually goes in the site-packages directory within your Python lib directory. 

setdefaultencoding function sets, well, the default encoding. This is the encoding scheme that Python will try to use whenever it needs to auto-coerce a unicode string into a regular string.

参考:

  1. https://docs.python.org/2/library/site.html
  2. http://www.cnblogs.com/dkblog/archive/2011/09/01/2161969.html
  3. http://blog.chinaunix.net/uid-22183602-id-2973101.html
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-04-27 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 参考:
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档