首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python笔记之2.x上兼容3.x版本

python笔记之2.x上兼容3.x版本

作者头像
py3study
发布2020-01-06 10:27:40
4780
发布2020-01-06 10:27:40
举报
文章被收录于专栏:python3python3

在前文《python笔记之3.x与2.x的使用区别》谈及了不同版本的区别问题。长远看软件新版本肯定会取代低版本的,除非你有成熟的老版本代码必须考虑兼容性问题,一般还是推荐新手学习新版本。

最近学习python,主要使用3.3版本,但看代码和书籍时,碰到多是2.x代码,感觉问题多多,尤其是处理中文的时候。我在win下主要使用py3自带的idle为编辑器。

下面在python 2.7.3下测试print显示汉字的问题。

第一个出场的是print语句版本:

#!/path/python #coding:utf-8 s = "汉字a1." ss = u"汉字a1." print s, type(s), len(s) print ss, type(ss), len(ss) print '-' * 40 print repr(s) print repr(ss) print '-' * 40 s1 = s.decode('utf-8') print s1,len(s1),type(s1) print '-' * 40 s2 = s.decode('utf-8').encode('gbk') print s2,type(s2),len(s2) print '-' * 40 s3 = ss.encode('gbk') print s3,type(s3),len(s3)

先分析下python代码的基本框架结构:

#!/path/python #coding:utf-8 # #

注意: 第1行:写明python路径,方便以后移植的Linux下,在windows下可以将path替换为合适的路径,如C:\python33。 第 2行:写明源码的编码格式,python 3就使用utf-8格式,python 2可以使用gbk/gb2312/cp936/gb18030几个同义语(实际上是有差别 的),方便中文处理。本行直接写“#coding:xyz”或“#coding=xys”都可以(其中xyz就是前面列举的你所使用的编码格式),完全不 必要加上花里胡哨的修饰,如“#-*- coding: utf-8 -*-”,纯粹的多此一举。 第3行开始:是写代码的地方,如import,def等等。

运行结果:

>>> 姹夊瓧a1. <type 'str'> 9 汉字a1. <type 'unicode'> 5 ---------------------------------------- '\xe6\xb1\x89\xe5\xad\x97a1.' u'\u6c49\u5b57a1.' ---------------------------------------- 汉字a1. 5 <type 'unicode'> ---------------------------------------- 汉字a1. <type 'str'> 7 ---------------------------------------- 汉字a1. <type 'str'> 7 >>> 

将上面的代码修改为print()函数版:

#!/path/python #coding:utf-8 s = "汉字a1." ss = u"汉字a1." print (s, type(s), len(s)) print( ss, type(ss), len(ss)) print('-' * 40) print(repr(s)) print( repr(ss)) print ('-' * 40) s1 = s.decode('utf-8') print (s1,len(s1),type(s1)) print ('-' * 40) s2 = s.decode('utf-8').encode('gbk') print (s2,type(s2),len(s2)) print( '-' * 40) s3 = ss.encode('gbk') print( s3,type(s3),len(s3))

执行结果:

>>> ('\xe6\xb1\x89\xe5\xad\x97a1.', <type 'str'>, 9) (u'\u6c49\u5b57a1.', <type 'unicode'>, 5) ---------------------------------------- '\xe6\xb1\x89\xe5\xad\x97a1.' u'\u6c49\u5b57a1.' ---------------------------------------- (u'\u6c49\u5b57a1.', 5, <type 'unicode'>) ---------------------------------------- ('\xba\xba\xd7\xd6a1.', <type 'str'>, 7) ---------------------------------------- ('\xba\xba\xd7\xd6a1.', <type 'str'>, 7) >>>

这是print语句与print函数输出不一致的一个例子。 可以看到,将print语句转换为print()后,显示结果乱七八糟,全是字符串的各种各样的ascii,gbk,unicode,utf-8编码,而非我所期望的汉字。 怎样才能低版本下利用高版本的特性呢,以提供未来升级后最大的可能性兼容呢?

python提供了自己特有的方案。在vpython.org网站看到:

Compatibility with the future and the past We encourage you to place the following statement at the start of your programs, in order that your program will run not only on Python 2.7 today, but also will run in the future on the newer Python 3.x series. Note the double underscore before "future" and the double underscore after "future". from __future__ import print_function, division For Python 2.7, this statement invokes the new Python 3.x print format, namely "print(x)" instead of the old "print x", and the new division scheme, namely that 3/4 is 0.75, not zero as in the past. This statement is ignored by Python 3.x.

简言之,在python 2.7的代码中,添加一句“from __future__ import print_function, division”,就可以确保低版本代码尽量兼容于python 3.3的代码。

注意future前后都有两个下划线“_”,不要搞错。 在前面的print()函数版代码中添加该句,如下文所示红色文字部分:

#!/path/python #coding:utf-8 from __future__ import print_function s = "汉字a1." ss = u"汉字a1." print (s, type(s), len(s)) print( ss, type(ss), len(ss)) print('-' * 40) print(repr(s)) print( repr(ss)) print ('-' * 40) s1 = s.decode('utf-8') print (s1,len(s1),type(s1)) print ('-' * 40) s2 = s.decode('utf-8').encode('gbk') print (s2,type(s2),len(s2)) print( '-' * 40) s3 = ss.encode('gbk') print( s3,type(s3),len(s3))

执行效果:

>>> 姹夊瓧a1. <type 'str'> 9 汉字a1. <type 'unicode'> 5 ---------------------------------------- '\xe6\xb1\x89\xe5\xad\x97a1.' u'\u6c49\u5b57a1.' ---------------------------------------- 汉字a1. 5 <type 'unicode'> ---------------------------------------- 汉字a1. <type 'str'> 7 ---------------------------------------- 汉字a1. <type 'str'> 7 >>>

这才是我所期望的效果,也解决了print语句与print函数输出不一致的问题。

最后看看除法(“/”)问题:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> print(3/4) 0 >>> print 3/4 0 >>> from __future__ import division >>> print(3/4) 0.75 >>> print 3/4 0.75 >>>

看到没有,有和没有“from __future__ import division”,导致所求结果大不同。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-09-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档