Python2到Python3代码的转换工具2to3.py
来源:互联网 发布:网络的3类地址 编辑:程序博客网 时间:2024/05/22 17:00
之前一直遇到Python2代码转到Python3代码的需求,最近才知道Python官方就有一个转换工具:2to3.py。
无论是Python官网下载安装的python还是利用Anaconda安装,目录都在{Python_HOME}\Tools\scripts里面。运行 2to3.py 脚本,打印如下:
python 2to3.py --helpUsage: 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 -o OUTPUT_DIR, --output-dir=OUTPUT_DIR Put output files in this directory instead of overwriting the input files. Requires -n. -W, --write-unchanged-files Also write files even if no changes were required (useful with --output-dir); implies -w. --add-suffix=ADD_SUFFIX Append this string to all output filenames. Requires -n if non-empty. ex: --add-suffix='3' will generate
假设我在E:\pycode目录下有个py2.py文件,代码如下:
print 'test'
我想把上面的代码改成python3版本的。、
应该先cmd进入{Python_HOME}\Tools\scripts目录,然后执行如下:
python 2to3.py -w E:\pycode\py2.py
执行整个流程如下:
D:\Anaconda3\Tools\scripts>python 2to3.py -w E:\pycode\py2.pyRefactoringTool: Skipping optional fixer: bufferRefactoringTool: Skipping optional fixer: idiomsRefactoringTool: Skipping optional fixer: set_literalRefactoringTool: Skipping optional fixer: ws_commaRefactoringTool: Refactored E:\pycode\py2.py--- E:\pycode\py2.py (original)+++ E:\pycode\py2.py (refactored)@@ -1 +1 @@-print 'test'+print('test')RefactoringTool: Files that were modified:RefactoringTool: E:\pycode\py2.py
然后我们可以看到,在E:\pycode目录下多了一个py2.py.bak文件,这是原来的py2.py文件备份。
查看py2.py,内容已被修改为:
print('test')
阅读全文
0 0
- Python2到Python3代码的转换工具2to3.py
- 使用2to3.py转换python2代码到python3
- 使用python3自带工具2to3.py 转换 python2.x 代码 到python3
- python3与python2的一些区别(使用2to3将代码移植到python3)
- 运用2to3.py将python2脚本转换为python3脚本
- python2转3格式自动工具:2to3 , 以及python3相对于python2 修改的一些内容
- python工具2to3.py的使用
- 使用 2to3 工具 转换python代码
- 浅谈2to3.py的使用
- 【Python】在PyCharm的菜单栏中配置Python2代码转换Python3的工具
- python中2to3工具的使用
- 使用2to3将代码移植到Python 3
- 使用2to3将代码移植到Python 3
- 使用2to3将代码移植到Python 3
- python2到python3的转换以及f.write在python3 中的用法
- 【教程】如何把Python2的代码转换为Python3的代码
- 将Anaconda 的python2升级到python3
- 从python2到python3
- [数据结构]数据结构——链表法
- Java练习
- LeetCode 515. Find Largest Value in Each Tree Row
- Sql 语句练习
- 第三周项目三 求集合并集
- Python2到Python3代码的转换工具2to3.py
- CSDN-markdown编辑器示意效果
- java程序练习:尝试创建一个父类,在父类中创建两个方法,在子类中覆盖第二个方法,为子类创建一个对象,将他向上转型到基类并调用这个方法。
- nginx配置location总结及rewrite规则写法
- JTable用法
- Navicat for mysql 下载、安装、连接MySQL操作
- HDU2222(AC自动机入门题)
- CentOS 7 yum 安装 Nginx
- Java练习