python编码转换
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编码转换的更多相关文章
- Python 编码转换与中文处理
python 中的 unicode是让人很困惑.比较难以理解的问题. 这篇文章 写的比较好,utf-8是 unicode的一种实现方式,unicode.gbk.gb2312是编码字符集. py文件中的 ...
- Python开发【第三章】:Python编码转换
一.字符编码与转码 1.bytes和str 之前有学过关于bytes和str之间的转换,详细资料->bytes和str(第四字符串) 2.为什么要进行编码和转码 由于每个国家电脑的字符编码格式不 ...
- python 编码转换(转)
主要介绍了python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换. 常见的编码转换分为以下几种情况: 自动识别 字符串编 ...
- python 编码转换 专题
主要介绍了python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换. 常见的编码转换分为以下几种情况: 自动识别 字符串编 ...
- Python之路3【知识点】白话Python编码和文件操作
Python文件头部模板 先说个小知识点:如何在创建文件的时候自动添加文件的头部信息! 通过:file--settings 每次都通过file--setings打开设置页面太麻烦了!可以通过:View ...
- 关于Python编码问题小记
Python编码问题小记: 引子: 最近在复习redis,当我在获取redis的key的时候,redis 存储英文和汉字下面这个样子的,我知道汉字是用16进制的UTF-8编码了,然后突然很想搞清楚字符 ...
- python编码总结
关于ASCII码和Unicode码的来源 计算机只能处理数字,如果要处理文本,需要先将文本转换成数字.早期计算机采用8bit作为一个字节(byte).所以一个字节最大为255(二进制11111111= ...
- Python 编码机制
python 编码转换 Python的编码机制,unicode, utf-8, utf-16, GBK, GB2312,ISO-8859-1 等编码之间的转换. 常见的编码转换分为以下几种情况: 自动 ...
- python 字符编码 转换
#!/bin/env python#-*- encoding=utf8 -*-# 文件头指定utf8编码还是乱码时,使用下面方式指定# fix encoding problem import sys ...
随机推荐
- C# 属性(Property)和字段(Field)的区别
导读: 近期学习过程中发现了一些问题,我的学习只是学习,敲代码就是敲代码,没有加入思考,也不问为什么就直接去敲人家写好的例子去敲,把知识都学死了,逐渐散失了思考能力,所以学习的兴趣大打折扣,正如那句话 ...
- 使用Docker方式运行Mysql(MariaDB)
两者差不多.我使用的是MariaDB. 下面的docker命令,挂了数据,配置,映射了端口,指定了root密码,服务端编码. 蛮快的! docker run \ --name mariadb \ -v ...
- Android设备一对多录屏直播--(UDP组播连接,Tcp传输)
原文:https://blog.csdn.net/sunmmer123/article/details/82734245 近期需要学习流媒体知识,做一个Android设备相互投屏Demo,因此找到了这 ...
- mysql binary
mysql在比较字符串的时候是忽略大些写的 比如有用户叫ABC和abc select * from `sys_user` where username = 'abc' 会出来两条记录 select * ...
- 【Android】让Python在Android系统上飞一会儿
第一节 在手机上配置Python运行环境 1.下载和安装 Scripting Layer for Android (SL4A) Scripting Layer for Android (SL4A) 是 ...
- Codeforces 877F Ann and Books 莫队
转换成前缀和, 预处理一下然后莫队. #include<bits/stdc++.h> #define LL long long #define fi first #define se se ...
- docker删除名字为none的imgae
docker rmi $(docker images -f "dangling=true" -q)
- P1026 统计单词个数 区间dp
题目描述 给出一个长度不超过200200的由小写英文字母组成的字母串(约定;该字串以每行2020个字母的方式输入,且保证每行一定为2020个).要求将此字母串分成kk份(1<k \le 401& ...
- 亲和串 kmp
Problem Description 人随着岁数的增长是越大越聪明还是越大越笨,这是一个值得全世界科学家思考的问题,同样的问题Eddy也一直在思考,因为他在很小的时候就知道亲和串如何判断了,但是发现 ...
- Zookeeper三个监听案例
一.监听某一节点内容 /** * @author: PrincessHug * @date: 2019/2/25, 14:28 * @Blog: https://www.cnblogs.com/Hel ...