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的更多相关文章

  1. Python——2x和3x的区别汇总

    1. 初始解释器编码: 2x:ascii 编码(不自持中文) 3x:unicode编码 推荐全部更换为utf-8 2. 输出方式不同 2x:print ‘你好’2.7版本的两种都支持 3x:print ...

  2. Python进阶之路---1.2python版本差异

    Python2.*与python3.*版本差异 作为一个初学者,我们应该如何选择python的版本进行学习呢,这两个版本有什么区别呢,接下来让我们简单了解一下,以便我们后续的学习. Python版本差 ...

  3. python 全栈开发,Day2(正式)

    一.in的使用 in 操作符用于判断关键字是否存在于变量中 a = '男孩wusir' print('男孩' in a) 执行输出: True in是整体匹配,不会拆分匹配. a = '男孩wusir ...

  4. python 全栈开发,Day1

    python基础一 一,Python介绍 python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为 ...

  5. python全栈学习--day2

    一.in的使用 说明:in有相当多的用处,比如判断,循环for 等. 实例一:in 操作符用于判断关键字是否存在于变量中 s = '男人john' print('男孩' in s) print('男孩 ...

  6. python全栈学习--day1

      计算机基础 CPU:中央处理器 内存:4GB,8GB,临时处理事务的地方,供给CPU数据. 硬盘:相当于电脑的数据库,存储着大量的数据,文件,电影等. 操作系统:执行者,支配所有关系 window ...

  7. python 全栈开发,Day2(in,while else,格式化输出,逻辑运算符,int与bool转换,编码)

    一.in的使用 in 操作符用于判断关键字是否存在于变量中 a = '男孩wusir' print('男孩' in a) 执行输出: True in是整体匹配,不会拆分匹配. a = '男孩wusir ...

  8. python 全栈开发,Day1(python介绍,变量,if,while)

    python基础一 一,Python介绍 python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为 ...

  9. Python全栈开发,Day2(in,while else,格式化输出,逻辑运算符,int与bool转换,编码)

    一.in的使用 in 操作符用于判断关键字是否存在于变量中 ? 1 2 a = '男孩wusir' print('男孩' in a) 执行输出: True in是整体匹配,不会拆分匹配. ? 1 2 ...

随机推荐

  1. mac idea中 maven项目添加的时候没有java文件

    file --other setting --maven  选中第二项即可  apply下

  2. 例子:Camera Color Picker Sample (YCbCr->ARGB)

    本例演示了如何从相机preview缓冲区获取YCbCr模块,并且转化为ARGB. 1. 什么是YCbCr y:像素的亮度.以范围从 0 到 255 的字节值形式返回(亮度值始终为正值). cr:像素的 ...

  3. C++之虚函数和多态

    干货较多-需要自己深思理解: C++支持两种多态性: 1.编译时多态性(静态绑定-早绑定) 在程序编译阶段即可以确定下来的多态性 通过使用 重载机制(重载函数)实现 (模板)http://blog.c ...

  4. boxsizing属性 IE盒模型和标准盒模型

    CSS3有一个非常有用但应用不广泛的属性: box-sizing: content-box | border-box | inherit content-box,默认属性,遵从标准盒模型. borde ...

  5. Ext JS 4 新特性2:配置项属性(config)之一

    Ext JS 4 新特征2:配置项属性config 最新版本的Ext JS 4.2的另外一个伟大的新特征就是增加了configuration配置项属性,当我们在创建一个新类的时候,经常性的要设置某某属 ...

  6. 运行html代码

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  7. readelf与动态库

    使用arm-linux-gcc编译的可执行文件可能会无法在开发板上执行,并提示:-/bin/sh xxx not found 解决办法: 在主机上使用readelf -d xxx 来查看该程序所需要的 ...

  8. 移动混合开发之HTML5在移动开发中的准则

    1.尽量单页面开发 2.慎重选择前端UI框架,新手最好自己动手. 3.动画特效达到60fps 4.长度单位是用rem,即字体的宽度,字体宽度可根据 window.width/number.

  9. 摆花(2012Noip普及组第3题)

    摆花 (flower.cpp/c/pas) [问题描述] 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共 m 盆.通过调查顾客的喜好,小明列出了顾客最喜欢的 n 种花,从 1 到 n ...

  10. css3 文字轮番滚动效果2——改进版

    1.优化了之前的代码: 2.修正了先前按照文字的条目的数量计算速度的问题,现在改为按照字符的个数计算动画执行一次需要的时间,更为精确: 3.增添了每一行JS代码的注释. 4.这个案例的用途一般为告警信 ...