Python2到Python3代码的转换工具2to3.py
2018-02-27 11:22
190 查看
之前一直遇到Python2代码转到Python3代码的需求,最近才知道Python官方就有一个转换工具:2to3.py。
无论是Python官网下载安装的python还是利用Anaconda安装,目录都在{Python_HOME}\Tools\scripts里面。运行 2to3.py 脚本,打印如下:
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
假设我在E:\pycode目录下有个py2.py文件,代码如下:
我想把上面的代码改成python3版本的。、
应该先cmd进入{Python_HOME}\Tools\scripts目录,然后执行如下:
执行整个流程如下:
2
3
4
5
6
7
8
9
10
11
12
13
然后我们可以看到,在E:\pycode目录下多了一个py2.py.bak文件,这是原来的py2.py文件备份。
查看py2.py,内容已被修改为:
无论是Python官网下载安装的python还是利用Anaconda安装,目录都在{Python_HOME}\Tools\scripts里面。运行 2to3.py 脚本,打印如下:
python 2to3.py --help 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 -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 generate1
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
假设我在E:\pycode目录下有个py2.py文件,代码如下:
print 'test'1
我想把上面的代码改成python3版本的。、
应该先cmd进入{Python_HOME}\Tools\scripts目录,然后执行如下:
python 2to3.py -w E:\pycode\py2.py1
执行整个流程如下:
D:\Anaconda3\Tools\scripts>python 2to3.py -w E:\pycode\py2.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 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.py1
2
3
4
5
6
7
8
9
10
11
12
13
然后我们可以看到,在E:\pycode目录下多了一个py2.py.bak文件,这是原来的py2.py文件备份。
查看py2.py,内容已被修改为:
print('test')
相关文章推荐
- 使用python3自带工具2to3.py 转换 python2.x 代码 到python3
- 使用Python3自带工具2to3.py 转换 Python2.x 代码 到Python3
- Python2到Python3代码的转换工具2to3.py
- Python和JavaScript间代码转换的4个工具
- 【Python】在PyCharm的菜单栏中配置Python2代码转换Python3的工具
- 四款工具顺利实现 Python 与 JavaScript 间的代码转换
- 四款工具顺利实现Python与JavaScript间的代码转换
- 实现Python与JavaScript代码转换的4个工具
- 使用 2to3 工具 转换python代码
- 使用2to3.py转换python2代码到python3
- 四款工具顺利实现Python与JavaScript间的代码转换
- python工具代码之农历转换公历,公历转换农历神器持续更新:【内向即失败--王奕君】
- VB.Net C#代码转换工具
- Python 提取dict转换为xml/json/table并输出的实现代码
- 使用python开发json、csv数据格式转换工具
- Python版代码混淆工具
- Python: UTF8转换代码实例
- html格式代码转换为code格式能在html内显示的代码转换工具
- Python代码高亮显示工具
- 在pycharm中配置从Python2.x到3.x的转换工具(新手向)