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 ...
随机推荐
- 常用CSS样式
1.line-height:行高.默认normal normal:允许内容顶开或溢出制定的容器边界; length:15px,可以为负数; ... 2.overflow:滚动条设置 overflow- ...
- Win7 64位ORACLE取数字乱码的解决
参见网址http://www.2cto.com/database/201304/201767.html 首先是PLSQL DEVELOPER 直接报错 NLS_LANG 错误 第一步是在命令行下测试 ...
- mseed2sac的安装和使用
由于使用rdseed提取mseed文件到SAC文件会遇到一个问题就是: 同时需要dataless文件: 因此如果下载的数据中恰巧没有dataless文件,则需要用另外一种方式:mseed2sac 现在 ...
- 第九章 硬件抽象层:HAL
这一章介绍HAL,全称为Hardware Abstract Layer,即硬件抽象层,它是建立在Linux驱动之上的一套程序库,程序库并不属于Linux内核,而是属于Linux内核层之上的应用层.为A ...
- thymeleaf的常见用法
1,th:属性名="",就可以直接修改控件的属性,比如 <input th:type="button" th:name="xinxin" ...
- 关于JavaEE 开发中web.xml的主要配置及其使用
web.xml 中的listener. filter.servlet 加载顺序及其详解 在项目中总会遇到一些关于加载的优先级问题,近期也同样遇到过类似的,所以自己查找资料总结了下,下面有些是转载其他人 ...
- Android 标题栏菜单设置与应用(popupWindow的应用)
效果图
- BZOJ 3176 Sort
先一遍reverse+逆序对个数. 要开long long啊. #include<iostream> #include<cstdio> #include<cstring& ...
- Makefile中静态库,动态库的创建和使用以及解压缩命令
应用层通过操作文件操控硬件 使用制作好的工具链: 刚开始学习时,用一些已经制作好的工具链,使用以下命令解压到gcc-3.4.5-glibc-2.3.6目录 cd /work/tools tar xjf ...
- Android 开发常用命令
1.生成keystore文件 keytool -exportcert -keystore keystore_path -list -v 2.查看APK签名 keytool -list -printce ...