• Python编码问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(12


    今天安装了PyScripter编辑器,刚要写代码,突然就出现异常:

    <span style="font-size:14px;color:#ff0000;">>>> Traceback (most recent call last):
    File "<string>", line 378, in findModuleOrPackage
    File "<string>", line 367, in find_dotted_module
    UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)</span>

    python在安装时,默认的编码是ascii。经常出现它无法处理非ascii编码的情况。此时需要手动修改它的编码字符集。

        查询系统默认编码:

        在Shell中,输入python,进入python解释器。
        >>>import sys 
        >>>sys.getdefaultencoding()

        即可。


        如果想设置默认编码: 

        >>>sys.setdefaultencoding('utf8')

        即可把默认编码设置为utf8。

        但是如果重启python解释器,默认编码就又变回ascii了。

        如果希望把系统默认编码永久设置为utf8,则:

        在python的Libsite-packages文件夹下新建一个sitecustomize.py,内容为: 

    # encoding=utf8
    import sys
    reload(sys)
    sys.setdefaultencoding('utf8')

    reference:

    http://blog.csdn.net/liuxingyu_21/article/details/27487391

    http://www.cnblogs.com/sislcb/archive/2008/11/26/1341453.html

    http://www.pythonfan.org/blog-3526-1416.html

  • 相关阅读:
    简单的NHibernate helper类,支持同一事务的批量数据处理
    外部唯一窗体
    Nhibernate常见的错误
    NHB下载地址
    oracle jdbc连接
    linux 中国发行版
    转:pl/sql develop的使用
    Oracle Database 10g Release 2 JDBC Drivers
    转:Setting up a Msysgit Server with copSSH on Windows
    oracle基础学习
  • 原文地址:https://www.cnblogs.com/missie/p/4397957.html
Copyright © 2020-2023  润新知