Python 2x -> 3.x
Nowadays, Python 3 is becoming more and more popular than Python 2, but there are still a lot of codes of Python 2 remained. Although we can use them on Python 2 or use pyenv to manage the versions of Python, but I still want to modify these codes to make them runnable in python 3.
2016/05/31: Hot, 22 - 34 ℃, Light rain, Tuesday
1.str and bytes(type in python)
In Python 2, str are given equal treatment to bytes, absolutly. In my opinion, I don't think it is good. String is a explicit entity of bytes, and bytes is inner entity of string.
We can easily declare a bytes value from a const string. Example: kom_header = b"KOG GC TEAM MASSFILE V.0.3."
Convert
str->decode->bytes
example: s = b'\x6d\x61\x69\x6B\x61\x7A\x65'
s.decode('utf-8')
bytes->encode->encoding
example: bytes(str, encoding = encoding_page)
Attention:
In Python 3, we should open a binary file in binary mode, instead of text mode.
2.xrange() and range()
In Python 2, there is some differences between xrange() and range(), which cause some unfair treatment to range()(QAQ,in perfermance),but now these differences are disappeared in Python 3. We can use range()(in fact, it is xrange()) only in Python 3, xrange() get the formal position to replace range().And range() function has died.(pupu).
Now range() will make a iterator (on rules) instead of a list.
Python 2x -> 3.x的更多相关文章
- Python——2x和3x的区别汇总
1. 初始解释器编码: 2x:ascii 编码(不自持中文) 3x:unicode编码 推荐全部更换为utf-8 2. 输出方式不同 2x:print ‘你好’2.7版本的两种都支持 3x:print ...
- Python进阶之路---1.2python版本差异
Python2.*与python3.*版本差异 作为一个初学者,我们应该如何选择python的版本进行学习呢,这两个版本有什么区别呢,接下来让我们简单了解一下,以便我们后续的学习. Python版本差 ...
- python 全栈开发,Day2(正式)
一.in的使用 in 操作符用于判断关键字是否存在于变量中 a = '男孩wusir' print('男孩' in a) 执行输出: True in是整体匹配,不会拆分匹配. a = '男孩wusir ...
- python 全栈开发,Day1
python基础一 一,Python介绍 python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为 ...
- python全栈学习--day2
一.in的使用 说明:in有相当多的用处,比如判断,循环for 等. 实例一:in 操作符用于判断关键字是否存在于变量中 s = '男人john' print('男孩' in s) print('男孩 ...
- python全栈学习--day1
计算机基础 CPU:中央处理器 内存:4GB,8GB,临时处理事务的地方,供给CPU数据. 硬盘:相当于电脑的数据库,存储着大量的数据,文件,电影等. 操作系统:执行者,支配所有关系 window ...
- python 全栈开发,Day2(in,while else,格式化输出,逻辑运算符,int与bool转换,编码)
一.in的使用 in 操作符用于判断关键字是否存在于变量中 a = '男孩wusir' print('男孩' in a) 执行输出: True in是整体匹配,不会拆分匹配. a = '男孩wusir ...
- python 全栈开发,Day1(python介绍,变量,if,while)
python基础一 一,Python介绍 python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为 ...
- Python全栈开发,Day2(in,while else,格式化输出,逻辑运算符,int与bool转换,编码)
一.in的使用 in 操作符用于判断关键字是否存在于变量中 ? 1 2 a = '男孩wusir' print('男孩' in a) 执行输出: True in是整体匹配,不会拆分匹配. ? 1 2 ...
随机推荐
- glibc与MSVC CRT(转载)
glibc与MSVC CRT 运行库是平台相关的,因为它与操作系统结合得非常紧密.C语言的运行库从某种程度上来讲是C语言的程序和不同操作系统平台之间的抽象层,它将不同的操作系统API抽象成相同的库函数 ...
- 012 VS2013常用快捷键
2016-01-28 1.回到上一个光标位置/前进到下一个光标位置 1)回到上一个光标位置:使用组合键“Ctrl + -”: 2)前进到下一个光标位置:“Ctrl + Shift + - ”. 2. ...
- Read excel and put cell data into HashMap
//Read excel row by row, put cell name and cell value to a map for each row. HashMap getExpectedResu ...
- MVC教程相关
本教程所有文章导航 本系列共10篇文章,翻译自Asp.Net MVC4 官方教程,由于本系列文章言简意赅,篇幅适中,从一个示例开始讲解,全文最终完成了一个管理影片的小系统,非常适合新手入门Asp.Ne ...
- 安全协议系列(五)---- IKE 与 IPSec(上)
IKE/IPSec 属于网络层安全协议,保护 IP 及上层的协议安全.自上个世纪末面世以来,关于这两个协议的研究.应用,已经非常成熟.协议本身,也在不断地进化.仅以 IKE 为例,其对应的 RFC 编 ...
- webpack vuejs项目学习心得
最近在做移动端的项目,最近webpack和vuejs很火,就想到了用vuejs webpack来构建我的项目 先了解了一些webpack的入门基础 http://webpack.github.io/d ...
- job1
http://www.newsmth.net/nForum/#!article/Career_Upgrade/221039 http://www.newsmth.net/nForum/#!articl ...
- Disable the screen switching about VI
If you want to disable the screen switching, and you don't want tochange your termcap, you can add t ...
- Back to openGL!
#include <iostream> #include <windows.h> #include <gl/glut.h> #include <math.h& ...
- 《HP大规模敏捷开发实践》读书笔记
读这本书的心得,敏捷是实践出来的,哪怕不懂srcum**等方法,只要坚持心中的价值观,朝一个方向改进,哪怕不能“任何时候都拥有符合发布要求的代码”,今天比昨天好,也是成功. 通过业务分析确定开 ...