zoukankan      html  css  js  c++  java
  • encode与decode,中文乱码

    https://blog.csdn.net/Y601343678/article/details/78585728 p> 

    如何获得系统的默认编码?

    #!/usr/bin/env python

    #coding=utf-8

    import sys

    printsys.getdefaultencoding()

    该段程序在英文WindowsXP上输出为:ascii

    该段程序在英文Windows7上输出为:mbcs

    在UliPad中运行如下代码:

    s=u"中文"

    print s

    会提示:UnicodeEncodeError:'ascii' codec can't encode characters in position 0-1: ordinal notinrange(128)。

    这是因为UliPad在英文WindowsXP上的控制台信息输出窗口是按照ascii编码输出的(英文系统的默认编码是ascii),

    而上面代码中的字符串是Unicode编码的,所以输出时产生了错误。

    将最后一句改为:prints.encode('gb2312')

    则能正确输出“中文”两个字。

    若最后一句改为:prints.encode('utf8')

    则输出:xe4xb8xadxe6x96x87,

    这是控制台信息输出窗口按照ascii编码输出utf8编码的字符串的结果。

    unicode(str,'gb2312')与str.decode('gb2312')是一样的,都是将gb2312编码的str转为unicode编码

    使用str.__class__可以查看str的编码形式

  • 相关阅读:
    Git :版本控制工具进阶
    Git 提交本地代码
    Git创建代码仓库
    Git
    SQLlite数据库的增删改查
    Android学习第十天
    Android学习第九天
    Android学习第八天
    Android学习第七天
    【k8s】Deployment
  • 原文地址:https://www.cnblogs.com/heluobing/p/11229255.html
Copyright © 2011-2022 走看看