使用python3自带工具2to3.py 转换 python2.x 代码 到python3

python自带的语言版本转换工具2to3.py使用起来很简单。
(1)使用Windows 命令提示符(cmd)打开2to3.py 脚本所在位置,如下图:
在这里插入图片描述

按指定代码转换

(2)在cmd的控制台环境下,先定位到你的2to3.py文件所在的路径,比如D:\Anaconda3\Tools\scripts包含了这个文件。然后输入命令:

D:\Anaconda3\Tools\scripts>python 2to3.py -w G:\Google下载\machinelearninginaction\Ch06\svmMLiA.py

G:\Google下载\machinelearninginaction\Ch06\svmMLiA.py文件为你要转换的文件的路径和名称

转换信息如下:把每一个需要转换的代码都进行转换。(中间转换的信息省略)

RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored G:\Google下载\machinelearninginaction\Ch06\svmMLiA.py
--- G:\Google下载\machinelearninginaction\Ch06\svmMLiA.py       (original)
+++ G:\Google下载\machinelearninginaction\Ch06\svmMLiA.py       (refactored)
................
................
................
RefactoringTool: Files that were modified:
RefactoringTool: G:\Google下载\machinelearninginaction\Ch06\svmMLiA.py
按目录转换(可以转换目录下的所有代码包括子文件夹下的.py文件)
D:\Anaconda3\Tools\scripts>python 2to3.py -w G:\Google下载\machinelearninginaction\Ch06\EXTRAS

G:\Google下载\machinelearninginaction\Ch06\EXTRAS文件夹为你要转换的文件夹的路径和名称
转换信息如下:

RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Refactored G:\Google下载\machinelearninginaction\Ch06\EXTRAS\notLinSeperable.py
--- G:\Google下载\machinelearninginaction\Ch06\EXTRAS\notLinSeperable.py        (original)
+++ G:\Google下载\machinelearninginaction\Ch06\EXTRAS\notLinSeperable.py        (refactored)
@@ -65,4 +65,4 @@
 ax.scatter(xcord1,ycord1, marker='o', s=50, c='red')
 plt.title('D')
 plt.show()
-print 'nima'
+print('nima')
RefactoringTool: No changes to G:\Google下载\machinelearninginaction\Ch06\EXTRAS\plotRBF.py
RefactoringTool: No changes to G:\Google下载\machinelearninginaction\Ch06\EXTRAS\plotSupportVectors.py
RefactoringTool: Files that were modified:
RefactoringTool: G:\Google下载\machinelearninginaction\Ch06\EXTRAS\notLinSeperable.py
RefactoringTool: G:\Google下载\machinelearninginaction\Ch06\EXTRAS\plotRBF.py
RefactoringTool: G:\Google下载\machinelearninginaction\Ch06\EXTRAS\plotSupportVectors.py
转换完之后,生成的新文件就会覆盖掉这个文件,生成的文件也就是3.0以上版本的python文件了
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值