如何把Python2的代码转换为Python3的代码
如何把Python2的代码转换为Python3的代码
如果对于python2和python3不熟悉的,可以参考:
【整理】总结Python2(Python 2.x版本)和Python3(Python 3.x版本)之间的区别
之前有机会接触到,将Python2的代码转换为Python3的代码。
经过一番折腾,大概有了基本概念了。
现在简要整理一下,关于如何将Python 2.x的代码,转换为Python 3.x的代码。
把Python 2.x的代码转换为Python 3.x代码的方法
1.自己手动转换
这个不必多说,如果只是涉及很少的函数,比如print等。
那么自己改改代码,也就可以了。
2.利用Python内置(Python脚本)工具,帮你自动转换
Python 2.x版本,比如我安装的Python 2.7.2,其在windows下载安装好之后,就自带了相关的一些有用的工具。
其中一个叫做2to3.py,就是用来帮你实现,将Python 2.x的代码,转换为Python 3.x的代码的。
其位置位于:Python安装的根目录\Python27\Tools\Scripts\2to3.py
【如何利用2to3.py,实现将Python 2.x的代码,转换为Python 3.x的代码】
比如我手上有个Python 2.x的python脚本:
D:\tmp\tmp_dev_root\python\python2_to_python3\34563264_data_from_site.py
现在,想要将其转换为Python 3.x的代码。
可以通过打开windows的cmd,定位至该要转换的脚本下,然后运行
D:\tmp\WordPress\DevRoot\Python27\Tools\Scripts\2to3.py -w 34563264_data_from_site.py
即可成功转换,对应的执行结果为:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
D:\tmp\tmp_dev_root\python\python2_to_python3>D:\tmp\WordPress\DevRoot\Python27\Tools\Scripts\2to3.py -w 34563264_data_from_site.pyRefactoringTool: Skipping implicit fixer: bufferRefactoringTool: Skipping implicit fixer: idiomsRefactoringTool: Skipping implicit fixer: set_literalRefactoringTool: Skipping implicit fixer: ws_commaRefactoringTool: Refactored 34563264_data_from_site.py--- 34563264_data_from_site.py (original)+++ 34563264_data_from_site.py (refactored)@@ -18,7 +18,7 @@ import time; import codecs; import logging;-import urllib;+import urllib.request, urllib.parse, urllib.error; from datetime import datetime,timedelta; from optparse import OptionParser; from string import Template,replace;@@ -90,7 +90,7 @@ foundPhone = eachItemSoup.find(attrs={"class":"phone"}); logging.debug("foundPhone=%s", foundPhone); if(foundPhone):- foundPhoneUni = unicode(foundPhone);+ foundPhoneUni = str(foundPhone); logging.debug("foundPhoneUni=%s", foundPhoneUni); # case 1: #<p class="phone"><strong>phone:</strong> 800.206.7886<br />@@ -122,7 +122,7 @@ foundWeb = eachItemSoup.find(attrs={"class":"web"}); logging.debug("foundWeb=%s", foundWeb); if(foundWeb):- foundWebUni = unicode(foundWeb);+ foundWebUni = str(foundWeb); logging.debug("foundWebUni=%s", foundWebUni); # <p class="web"><strong>e-mail:</strong> <a href="#">sales@cinesysinc.com</a><br />@@ -151,7 +151,7 @@ foundAddr = eachItemSoup.find(attrs={"class":"addr"}); logging.debug("foundAddr=%s", foundAddr); if(foundAddr):- foundAddrUni = unicode(foundAddr);+ foundAddrUni = str(foundAddr); # <p class="addr"> # <strong>address:</strong> 740 SW 21st Ave, Suite #310<br />RefactoringTool: Files that were modified:RefactoringTool: 34563264_data_from_site.py |
此时,你可以看到原先的34563264_data_from_site.py,已经变成了Python 3.x的代码了。
对应的,也多出一个bak文件:34563264_data_from_site.py.bak,两者比较一下,即可看出区别:
![]()
当前,对于2to3.py本身,也可以通过help查看到更多的用法:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
D:\tmp\tmp_dev_root\python\python2_to_python3>D:\tmp\WordPress\DevRoot\Python27\Tools\Scripts\2to3.py -hUsage: 2to3 [options] file|dir ...Options: -h, --help show this help message and exit -d, --doctests_only Fix up doctests only -f FIX, --fix=FIX Each FIX specifies a transformation; default: all -j PROCESSES, --processes=PROCESSES Run 2to3 concurrently -x NOFIX, --nofix=NOFIX Prevent a transformation from being run -l, --list-fixes List available transformations -p, --print-function Modify the grammar so that print() is a function -v, --verbose More verbose logging --no-diffs Don't show diffs of the refactoring -w, --write Write back modified files -n, --nobackups Don't write backups for modified files |
此处只多解释几句:
(1)如果上述不加-w参数,则默认只是把转换过程所对应的diff内容打印输出到当前窗口而已。
(2)加了-w,就是把改动内容,写回到原先的文件了。
(3)不想要生成bak文件,再加上-n即可。
(4)不想看到那一堆输出的内容,加上–no-diffs,即可。
其他的,就不多介绍了。感兴趣的可以自己去继续折腾。
如何把Python2的代码转换为Python3的代码的更多相关文章
- python2.x脚本转换为python3.x脚本的方法
版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/dushu990/article/details/73549174 python2.x脚本转换为pyt ...
- python 内置2to3工具将python2代码转换为python3代码
python2与python3代码不兼容,如果需要python2代码在python3环境下运行,需要将代码进行转换,本文介绍使用python3内置工具2to3.py对代码进行转换 一:2to3.py在 ...
- ios oc 代码 转换为 c++ 描述代码编译过程
clang -rewrite-objc main.m #import <Foundation/Foundation.h> #import <objc/runtime.h> // ...
- 将python2代码转为python3
将python2代码转为python3 1.2to3在anaconda的/bin文件夹下: 2.打印帮助信息 2to3 --help 3.使用2to3 -W [要转换的python2文件目录] 4.转 ...
- 使用Python3自带工具2to3.py 转换 Python2.x 代码 到Python3
几乎所有的Python 2程序都需要一些修改才能正常地运行在Python 3的环境下.为了简化这个转换过程,Python 3自带了一个叫做2to3的实用脚本(Utility Script),这个脚本会 ...
- 再见,Python2。你好,Python3
文章首发自我的公众号,转载请注明出处~ Python2的退场,意味着一个时代的结束 我们这一代程序员基本都接触过python2,很多人也是从python2时代一路走来的.但是,是时候说再见了 ...
- Python中生成器和迭代器的区别(代码在Python3.5下测试):
https://blog.csdn.net/u014745194/article/details/70176117 Python中生成器和迭代器的区别(代码在Python3.5下测试):Num01–& ...
- 学python2.7简单还是python3.0简单,两者区别
学python2.7简单还是python3.0简单,谈谈两者区别 1. 使用__future__模块 Python 3.X 引入了一些与Python 2 不兼容的关键字和特性.在Python 2中,可 ...
- [.NET] 怎样使用 async & await 一步步将同步代码转换为异步编程
怎样使用 async & await 一步步将同步代码转换为异步编程 [博主]反骨仔 [出处]http://www.cnblogs.com/liqingwen/p/6079707.html ...
随机推荐
- Linux释放内存
在Linux系统下,我们一般不需要去释放内存,因为系统已经将内存管理的很好.但是凡事也有例外,有的时候内存会被缓存占用掉,导致系统使用SWAP空间影响性能,此时就需要执行释放内存(清理缓存)的操作了. ...
- BufferedStream类 - 缓冲流
BufferedStream常用于对其他流的一个封装,它必须和其他流结合一起使用.MemoryStream将所有的内容都放入内存中,而BufferedStream不是.BufferedStream在基 ...
- java设计模式--行为型模式--中介者模式
怎么理解中介者模式,我姑且用房产中介来理解吧.呵呵 中介者模式: 中介者模式 概述 用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显式地相互引用,从而使其耦合松散,而且可以独立地改变它们之 ...
- form-validation-engine中的正则表达式
form-validation-engine是一个不错的表单验证,可以玩玩. (function($) { $.fn.validationEngineLanguage = function() {}; ...
- mvc 解决StyleBundle中 图片绝对路径 装换成相对路径的问题 CssRewriteUrlTransform
问题 解决办法
- HBase 3、HBase练习题
1.建立学生和课程表 要求:学生可以选择多个课程,每个课程可以被多个学生选择. 查询某个学生所选的所有课程列表 查询某个课程,的学生列表 学生可以修改所选的课程 方案:学生与课程之间是多对多关系,那可 ...
- IOS 网络判断
Reachability *connectionNetWork= [Reachability reachabilityForInternetConnection] ; int status = [co ...
- 修改linux共享内存大小
这是实际linux系统显示的实际数据: beijibing@bjb-desktop:/proc/sys/kernel$ cat shmmax 33554432 beijibing@bjb-deskt ...
- php各种编译错误汇总
PHP编译安装时常见错误解决办法,php编译常见错误 This article is post on https://coderwall.com/p/ggmpfa configure: error: ...
- 9. iptables 配置
iptables 配置文件存放位置: [root@Demon yum.repos.d]# vim /etc/rc.d/init.d/iptables 一.只给 Centos 6.5 打开 22 ...