Pyton内部的字符串一般都是unicode编码或字节字符串编码;
代码中字符串的默认编码与代码文件本身的编码是一致的;
编码转换通常需要以unicode编码作为中间编码进行转换,即先将其他编码的字符串解码(decode)成unicode字符串,再从unicode编码(encode)成需要的编码;

编码和解码的方式要一致;

不同运行环境的默认编码也可能不一样;dos下默认是:ascii(gbk)
dos环境下:

1.获取系统默认编码:

>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>>

字节字符串:
>>> s="abc"
>>> type(s)
<type 'str'>

unicode字符串:
>>> s=u"中文"
>>> type(s)
<type 'unicode'>

2.英文字符串编码转换:英文字符串可以decode或encode(除unicode外)任何需要的编码

>>> s="abc"    #英文可以decode和encode(除unicode外)任何需要的编码
>>> s.decode()
u'abc'
>>> s.decode("gbk")
u'abc'
>>> s.decode("ascii")
u'abc'
>>> s.decode("utf-8")
u'abc'
>>> s.decode("gb2312")
u'abc'
>>> s.decode("unicode")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: unicode

>>> s="abc"    #英文可以decode和encode(除unicode外)任何需要的编码
>>> s.encode()
'abc'
>>> s.encode("gbk")
'abc'
>>> s.encode("ascii")
'abc'
>>> s.encode("utf-8")
'abc'
>>> s.encode("gb2312")
'abc'
>>> s.encode("unicode")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: unicode
>>>

>>> s=u"abc"    #英文可以decode和encode(除unicode外)任何需要的编码
>>> s.decode()
u'abc'
>>> s.decode("gbk")
u'abc'
>>> s.decode("ascii")
u'abc'
>>> s.decode("utf-8")
u'abc'
>>> s.decode("gb2312")
u'abc'
>>> s.decode("unicode")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: unicode

>>> s=u"abc"    #英文可以decode和encode(除unicode外)任何需要的编码
>>> s.encode()
'abc'
>>> s.encode("gbk")
'abc'
>>> s.encode("ascii")
'abc'
>>> s.encode("utf-8")
'abc'
>>> s.encode("gb2312")
'abc'
>>> s.encode("unicode")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: unicode
>>>

3.中文编解码:

(1)dos环境下默认编码是gbk,所以只能decode(gbk/gb2312)

(2)unicode编码的中文只能encode,不能decode;

>>> s="中文"  #dos的默认编码是gbk,所以此例只能decode(gbk/gb2312)
>>> s.decode()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in position 0: ordinal
not in range(128)
>>> s.decode("gbk")
u'\u4e2d\u6587'
>>> s.decode("ascii")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in position 0: ordinal
not in range(128)
>>> s.decode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xd6 in position 0: invalid c
ontinuation byte
>>> s.decode("gb2312")
u'\u4e2d\u6587'
>>> s.decode("unicode")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: unicode
>>>

>>> s="中文"    #dos的默认编码是gbk,所以此例只能先decode(gbk/gb2312),再encode成需要的编码
>>> s.encode()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in position 0: ordinal
not in range(128)
>>> s.encode("gbk")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in position 0: ordinal
not in range(128)
>>> s.encode("ascii")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in position 0: ordinal
not in range(128)
>>> s.encode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in position 0: ordinal
not in range(128)
>>> s.encode("gb2312")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in position 0: ordinal
not in range(128)
>>> s.encode("unicode")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: unicode
>>>

>>> s=u"中文"  #unicode编码的中文只能encode,不能再decode
>>> s.decode()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> s.decode("gbk")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> s.decode("ascii")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> s.decode("utf-8")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Python27\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> s.decode("gb2312")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> s.decode("unicode")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: unicode
>>>

>>> s=u"中文"    #unicode编码的中文只能encode,不能再decode
>>> s.encode()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> s.encode("gbk")
'\xd6\xd0\xce\xc4'
>>> s.encode("ascii")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordin
al not in range(128)
>>> s.encode("utf-8")
'\xe4\xb8\xad\xe6\x96\x87'
>>> s.encode("gb2312")
'\xd6\xd0\xce\xc4'
>>> s.encode("unicode")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
LookupError: unknown encoding: unicode
>>>

python编码转换的更多相关文章

  1. Python 编码转换与中文处理

    python 中的 unicode是让人很困惑.比较难以理解的问题. 这篇文章 写的比较好,utf-8是 unicode的一种实现方式,unicode.gbk.gb2312是编码字符集. py文件中的 ...

  2. Python开发【第三章】:Python编码转换

    一.字符编码与转码 1.bytes和str 之前有学过关于bytes和str之间的转换,详细资料->bytes和str(第四字符串) 2.为什么要进行编码和转码 由于每个国家电脑的字符编码格式不 ...

  3. python 编码转换(转)

    主要介绍了python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换. 常见的编码转换分为以下几种情况: 自动识别 字符串编 ...

  4. python 编码转换 专题

    主要介绍了python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换. 常见的编码转换分为以下几种情况: 自动识别 字符串编 ...

  5. Python之路3【知识点】白话Python编码和文件操作

    Python文件头部模板 先说个小知识点:如何在创建文件的时候自动添加文件的头部信息! 通过:file--settings 每次都通过file--setings打开设置页面太麻烦了!可以通过:View ...

  6. 关于Python编码问题小记

    Python编码问题小记: 引子: 最近在复习redis,当我在获取redis的key的时候,redis 存储英文和汉字下面这个样子的,我知道汉字是用16进制的UTF-8编码了,然后突然很想搞清楚字符 ...

  7. python编码总结

    关于ASCII码和Unicode码的来源 计算机只能处理数字,如果要处理文本,需要先将文本转换成数字.早期计算机采用8bit作为一个字节(byte).所以一个字节最大为255(二进制11111111= ...

  8. Python 编码机制

    python 编码转换 Python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换. 常见的编码转换分为以下几种情况: 自动 ...

  9. python 字符编码 转换

    #!/bin/env python#-*- encoding=utf8 -*-# 文件头指定utf8编码还是乱码时,使用下面方式指定# fix encoding problem import sys ...

随机推荐

  1. Junit4 IDEA测试学习一

    1.Junit4 下载 https://github.com/junit-team/junit4/releases 4.12 还需要还导入hamcrest-core-1.3.jar 2.IDEA Te ...

  2. 用vi编辑文件

    原文:https://www.ibm.com/developerworks/library/l-lpic1-103-8/index.html Overview In this article, lea ...

  3. Inflated 3D ConvNet 【I3D】

    Two-Stream Inflated 3D ConvNet (I3D) HMDB-51: 80.9% and UCF-101: 98.0% 在Inception-v1 Kinetics上预训练 Co ...

  4. csv导入数据到mongodb3.2

    mongoimport.exe -d paper -c paper K:\paper.csv --type csv -f id,name 数据库名 表名      文件所在位置      文件类型   ...

  5. Python初次安装使用教程

    Python官网: https://www.python.org/downloads/ 当前版本为3.7.0 下载(64位系统)exe文件进行安装. 双击安装运行 选择自定义安装路径         ...

  6. Netty断线重连

    Netty断线重连 最近使用Netty开发一个中转服务,需要一直保持与Server端的连接,网络中断后需要可以自动重连,查询官网资料,实现方案很简单,核心思想是在channelUnregistered ...

  7. 1900型USB接口扫描枪设置虚拟串口模式提升扫描速度

    在使用扫描枪的过程中,发现扫描二维码速度比较慢,不到100个字符,花了大概2-3秒的时间才完成显示,这个速度不能忍受啊.通过度娘,说是可以将USB键盘模式接收字符转换成虚拟串口接收,这样可以大大提高速 ...

  8. 今天才知道原来我还没弄清楚js中全局变量和局部变量的定义...

    查资料看到这段还不错,来源:原文:https://blog.csdn.net/czh500/article/details/80429133 粘过来记录一下... 1.使用var声明变量,在方法内部是 ...

  9. 自动驾驶系统 bfs

    一家科技公司有一块试验地用于测试自动驾驶系统.试验地由n×m个格子组成,从上到下依次编号为第1到n行,从左到右依次编号为第1到m列.试验车位于其中的某个格子上,每次自动驾驶系统可以控制汽车往上下左右移 ...

  10. 012 pandas与matplotlib结合制图

    这里以后再补充. 1.折线图