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. logsatsh input 插件之 collectd

    logsatsh input 插件之 collectd 标签(空格分隔): logstash 作用:用于监控内存,cpu,磁盘I等信息 未完待续,时间未定. 参考: logstash 官网 elast ...

  2. DataTable 删除列 调整列顺序 修改列标题名称

    DataTable dt = new DataTable(); //删除列 dt.Columns.Remove("Sex"); dt.Columns.Remove("Ag ...

  3. 移动端div移动

    <!doctype html> <html lang="en"> <head> <meta http-equiv="Conten ...

  4. EL和JSTL专题

    EL简介 EL全名为Expression Language,它原本是JSTL 1.0为方便存取数据所自定义的语言.当时只能在JSTL标签中使用,如下: <c:out value="${ ...

  5. 【python】实用函数啥的

    1.测试运行时间/效率 t = time() print time() -t 2.C:\python344\Scripts   easy_install.exe ,提供包的名字,可以自动下载+装包 3 ...

  6. Spark源码学习1.8——ShuffleBlockManager.scala

    shuffleBlockManager继承于Logging,参数为blockManager和shuffleManager.shuffle文件有三个特性:shuffleId,整个shuffle stag ...

  7. 纠结attr(),prop()

    刚刚看博客无意中看到attr()和prop()的区别,回头就去翻了一下手册,感觉手册上写的过于简单,不能很清晰的分辨出两者的区别,两者的参数用法都是高度相似. attr():设置或返回被选元素的属性值 ...

  8. keyFile 巩固练习

    系统 : Windows xp 程序 : noodles-crackme2 程序下载地址 :http://pan.baidu.com/s/1mhJ4Ems 要求 : 编写KeyFile 使用工具 : ...

  9. Linux下memcache的安装

    memcache是高性能,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度.目前用memcache解决互联网上的大用户读取是非常流行的一种用法,在互联网企业中有着广泛的应用.‍关 ...

  10. PowerDesigner自增列问题