美文网首页
python2编码

python2编码

作者: cotecc | 来源:发表于2018-03-16 13:59 被阅读0次

一、概念

python2默认ascii码,可以在文件头显式指定编码(utf-8,此时所有str的编码都是utf-8)
python2底层两种编码:

  • str,二进制编码
  • unicode,与存储无关的编码
#coding:gb2312
ss="你好"                #使用gb2312编码的str
print type(ss) 
ss=ss.decode('gb2312')   #解码成unicode
print type(ss)

#coding:utf-8
ss="你好"                #使用utf8编码的str
print type(ss)
ss=ss.decode("utf8")     #解码成unicode
print type(ss)

#coding:utf-8
ss=u"你好"               #unicode
print type(ss)
ss=ss.encode("utf-8")    #编码成unicode
print type(ss)

二、chardet查看编码
chardet接受二进制编码输入,输出对应的编码类型

#coding:utf-8
import chardet

ss="你好"                                      #utf-8
str_type = chardet.detect(ss)  
print str_type['encoding']              #utf-8

相关文章

  • Python2编码问题

    Python2 源码编码 python2源码默认使用ascii进行编码,当源码中出现中文字符等非ascii编码的字...

  • 第2章 - 基础入门.md

    第2章 基础入门 2.1 Python 中文编码 Python2 Python2 文件如果未指定编码,在执行过程会...

  • 转自 segmentfault<python编码的意义&g

    原文见python编码的意义,感谢jiminhuang大神 编码,还是编码 python2的直钩——编码异常 当你...

  • Python——文件编码

    编码的演变 Python编码 python2 Python指定编码 在文件头部增加 -*-coding:utf8-...

  • Python2和Python3的区别

    编码方式 编码 python2的默认编码是ascii,所以会导致经常会遇到编码问题文件中经常会写# coding=...

  • Python基本知识

    一,解释器默认编码的区别   Python2解释器默认编码:ASCII  Python3解释器默认编码:UTF8 ...

  • python 编码问题总结记录

    Python2的默认编码ASCII,这是python编码问题的根本原因,可以想象,python3的编码问题肯定没有...

  • PY2和PY3的区别

    1.编码 python2默认编码方式ASCII码(不能识别中文,要在文件头部加上 #-- encoding:ut...

  • 编码 -- python2

    机器码到字符(中文字符、英文字符...)的转换关系 ASCII ASCII(American Standard C...

  • python2编码

    一、概念 python2默认ascii码,可以在文件头显式指定编码(utf-8,此时所有str的编码都是utf-8...

网友评论

      本文标题:python2编码

      本文链接:https://www.haomeiwen.com/subject/renlqftx.html