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. JavaBean toString() - 将bean对象打印成字符串

    JavaBean toString方式 https://www.cnblogs.com/thiaoqueen/p/7086195.html //方法一:自动生成 @Override public St ...

  2. Linux 下压缩与解压.zip和.rar

    )对于.zip linux下提供了zip和unzip程序,zip是压缩程序,unzip是解压程序.它们的参数选项很多,可用命令zip -help和unzip -help查看,这里只做简单介绍,举例说明 ...

  3. Pytorch LSTM 词性判断

    首先,我们定义好一个LSTM网络,然后给出一个句子,每个句子都有很多个词构成,每个词可以用一个词向量表示,这样一句话就可以形成一个序列,我们将这个序列依次传入LSTM,然后就可以得到与序列等长的输出, ...

  4. 一起学Hadoop——二次排序算法的实现

    二次排序,从字面上可以理解为在对key排序的基础上对key所对应的值value排序,也叫辅助排序.一般情况下,MapReduce框架只对key排序,而不对key所对应的值排序,因此value的排序经常 ...

  5. Kudu的卸载(cdh)

    卸载kudu 1):删除kudu相关包 rm -rf $(find / -name "*kudu*") 2):卸载kudu相关依赖 查询节点的kudu依赖: rpm -qa | g ...

  6. Codeforces 1139E Maximize Mex 二分图匹配

    Maximize Mex 离线之后把删数变成加数, 然后一边跑匈牙利一遍算答案. #include<bits/stdc++.h> #define LL long long #define ...

  7. P1026 统计单词个数 区间dp

    题目描述 给出一个长度不超过200200的由小写英文字母组成的字母串(约定;该字串以每行2020个字母的方式输入,且保证每行一定为2020个).要求将此字母串分成kk份(1<k \le 401& ...

  8. 三色抽卡游戏 博弈论nim

    你的对手太坏了!在每年的年度三色抽卡游戏锦标赛上,你的对手总是能打败你,他的秘诀是什么? 在每局三色抽卡游戏中,有n个卡组,每个卡组里所有卡片的颜色都相同,且颜色只会是红(R).绿(G).蓝(B)中的 ...

  9. Python中type和object

    type  所有类是type生成的 a = 1 b = "abc" print("type a:{}".format(type(a))) print(" ...

  10. C++中全排列算法函数next_permutation的使用方法

    首先,先看对next_permutation函数的解释: http://www.cplusplus.com/reference/algorithm/next_permutation/?kw=next_ ...