编码那些事:

乱码问题一直是开发中比较常见的问题,特别是在 windows 平台下开发,会经常遇到,其实乱码产生的原因就是编码与解码的方式不一致造成的,在 windows 下默认使用的是 GBK 编码,而开发时普遍使用的是 utf-8。在 IDE 这类内置控制台运行的时候,控制台的默认编码就是 GBK(即使你通过注册表修改了控制台的默认编码,默认情况下 IDE 的默认编码仍旧是会是 GBK) ,所以一但出现中文等非 ASCII 输出,就会出现乱码。所以有些时候出现你所看到的乱码并不见得是你的程序内部编码转换出现问题。

关于windows 下的乱码分析和如何修改控制台的编码 (l临时/注册表永久) ,请参考我的这篇文档:

https://blog.csdn.net/gulang03/article/details/81771343

关于常见的几种编码简单介绍参看:

https://blog.csdn.net/gulang03/article/details/79328868

Python3 中 编码(encode) 与 解码 ( decode ) :

str.encode():

源码:

def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__"""S.encode(encoding='utf-8', errors='strict') -> bytesEncode S using the codec registered for encoding. Default encodingis 'utf-8'. errors may be given to set a different errorhandling scheme. Default is 'strict' meaning that encoding errors raisea UnicodeEncodeError. Other possible values are 'ignore', 'replace' and'xmlcharrefreplace' as well as any other name registered withcodecs.register_error that can handle UnicodeEncodeErrors."""return b""

从源码相比可以清楚的看到,其返回值是二进制(binary)的 bytes。所以顾名思义 encode 的作用就是将 str 采用指定的编码方式解码,并返回编码后的 byte 数组。从源码中可以看出其默认的编码方式就是 UTF-8 。也就是 Python3 默认的编码方式。Python2 之前并不是如此。

bytes.decode():

源码:

    def decode(self, *args, **kwargs): # real signature unknown"""Decode the bytes using the codec registered for encoding.encodingThe encoding with which to decode the bytes.errorsThe error handling scheme to use for the handling of decoding errors.The default is 'strict' meaning that decoding errors raise aUnicodeDecodeError. Other possible values are 'ignore' and 'replace'as well as any other name registered with codecs.register_error thatcan handle UnicodeDecodeErrors."""pass

decode 顾名思义就是解码喽,就是将 bytes 按照指定参数解码成 String 对象。

Python3 的 encode 与 decode相关推荐

  1. python3 的encode 和 decode

    str ----> bytes: encode 编码 bytes----> str: decode 解码 str.encode() bytes.decode() >>> ...

  2. python中encode用法_python3中encode和decode的一些基本用法

    python3中encode和decode跟python2还是有一定的区别的,在python3中: encode(编码):按照某种规则将"文本"转换为"字节流" ...

  3. python3 gb2312转utf8_python2和python3字符编码 unicode,utf-8,gb2312相互转换encode,decode...

    一.预习: 1.Unicode 和utf-8之间的关系 unicode是一种编码标准,具体的实现标准可能是utf-8,utf-16,gbk -- UTF-8是Unicode的实现方式之一 2.pyth ...

  4. decode函数python在哪里_Python基础知识——encode和decode函数

    以前我们介绍过,Python2.x中默认的编码的基础类型是unicode编码的类型,在Python3.x才转化为基于unicode的字符串. 那么我们在Python2.x的学习中就会遇到各种各样的编码 ...

  5. [转]python新手必碰到的问题---encode与decode,中文乱码--转载

    edu.codepub.com/2009/1029/17037.php 这个问题在python3.0里已经解决了. 这有篇很好的文章,可以明白这个问题: 为什么会报错"UnicodeEnco ...

  6. 2019年2月26日 Unique Email Addresses、To Lower Case、Encode and Decode TinyURL

    今天开始加快速度,趁着还有空多刷几题,语言换成python提高速度了. 1. Unique Email Addresses 弱题,注意@符号前后的处理方式不同 class Solution(objec ...

  7. pythonunicode和str_python的str,unicode对象的encode和decode方法

    python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byt ...

  8. python中的encode()和decode()函数

    对于很多人来说,python的中字符转码是一件很头疼的事情,本来期望结果输出的是中文,结果来一段像这样\xe4\xbd\xa0\xe5\xa5\xbd像是乱码的字符串. 由于学python没多久,昨天 ...

  9. Python zipfile 文件名称编码 file_name.encode(‘cp437‘).decode(‘gbk‘)

    主要是处理压缩包里的中文,不出现乱码 with zipfile.ZipFile('love.zip', 'r') as zipobj: #读取压缩包for file_name in zipobj.na ...

最新文章

  1. linux日志汇总,Linux日志分析常用命令汇总(示例代码)
  2. Paddle.js PaddleClas 实战 ——『寻物大作战』AI 小游戏
  3. 文件上传之Springmvc方式上传代码
  4. Spark官方调优文档翻译(转载)
  5. ASP.NET Core + Angular 2 Template for Visual Studio
  6. SQLAlchemy 教程 —— 基础入门篇
  7. java如何排除多余的依赖_Maven依赖排除 禁止依赖传递 取消依赖的方法 去除jar包中的不想要的依赖关系...
  8. [笔记二]Essential JavaScript Design Patterns For Beginners
  9. Java日期练习—计算已出生多少天
  10. 网页调用OCX控件总结
  11. java bigdecima_Java收藏排序BigDecimal
  12. 企业微信推送消息(文本)
  13. pip install清华镜像源
  14. 当电脑内存比较小的时候,小于4G,安装32bit还是64bit呢
  15. html图片加水印元素,使用HTML5 Canvas给图片加水印效果
  16. SpringMVC 学谈 (第二章)
  17. 可以下载《全程软件测试》样章电子版
  18. 【网络运维】小平头PingTow网络IP导入检测工具软件开发源代码分享
  19. 青龙 金手指教程每天低保保姆安装教程
  20. cocos2d video视频层放置ui

热门文章

  1. zip: stdin: unexpected end of file tar: 归档文件中异常的 EOF tar: 归档文件中异常的 EOF tar: Error is not recoverable
  2. 八、马科维茨投资组合
  3. 有望取代Spark,Michael Jordan和Ion Stoica提出下一代分布式实时机器学习框架Ray牛在哪?...
  4. 直播 | SemEval-2020自由文本关系抽取冠军方案解读(附NLP竞赛常用技巧总结)
  5. 【Java代码】实现字符串转数据库的 inStr【使用 JDK8 stream.collect(Collectors.joining(delimiter, prefix, suffix)) 实现】
  6. python mysqldb安装_解决centos7 安装MySQLdb-python 报错 方案
  7. 一行文章让你搞懂什么是前缀、中缀、后缀表达式以及它们之间的相互转换
  8. 表达式封装和模型驱动封装的区别
  9. SpringAOP中通过JoinPoint获取值,并且实现redis注解
  10. LeetCode 25 K个一组翻转链表