Python 2x -> 3.x
2016-05-31 17:50
323 查看
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.
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.
相关文章推荐
- 图像处理DOG 算法,python结合cv2实现
- Python【8】-分析json文件
- Python数据分析入门
- Python 编码风格指南
- ERROR (ProgrammingError) (2014, "Commands out of sync; you can't run this command now")
- Google开源的Python格式化工具YAPF的安装和使用教程
- Python 3.0最简单的爬虫
- python学习 流程控制语句
- Python中Collections模块的Counter容器类使用教程
- Python中用MacFSEvents模块监视MacOS文件系统改变一例
- 欧几里德算法及python脚本实现
- yum中的python版本错误(转载)
- gensim安装
- Python的Django应用程序解决AJAX跨域访问问题的方法
- python中的urlencode和urldecode 【中文和url编码转换】
- python 连接各类主流数据库简单示例
- python2.0 s12 day8 _ socketserver学习
- [python] 转换python脚本程序为二进制ELF
- python学习网站
- Python核心编程(第十二章)--模块