python3代码转python2_【教程】如何把Python2的代码转换
为Python3的代码
把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
即可成功转换,对应的执⾏结果为:
D:\tmp\tmp_dev_root\python\python2_to_python3>D:\tmp\WordPress\DevRoot\Python27\Tools\Scripts\2to3.py -w 34563264_data_from_site.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: 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;
+quest, urllib.parse, ;
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:
#
phone: 800.206.7886
@@ -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);
#
e-mail:
@@ -151,7 +151,7 @@
foundAddr = eachItemSoup.find(attrs={"class":"addr"});
logging.debug("foundAddr=%s", foundAddr);
if(foundAddr):
python代码转换- foundAddrUni = unicode(foundAddr);
+ foundAddrUni = str(foundAddr);
#
# address: 740 SW 21st Ave, Suite #310
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查看到更多的⽤法:
D:\tmp\tmp_dev_root\python\python2_to_python3>D:\tmp\WordPress\DevRoot\Python27\Tools\Scripts\2to3.py -h
Usage: 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,即可。
其他的,就不多介绍了。感兴趣的可以⾃⼰去继续折腾。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论