python 3to2 2to3-python3转python2 python2转python3

python - 3to2 | 2to3 - python3转python2 | python2转python3


注意:文件名不要带中文。

3转2

3to2

安装

# 安装3to2
$ pip3 install 3to2 

使用

# 显示帮助
$ 3to2 -h 
Usage: 3to2 [options] file|dir ...
# 转换单个文件
$ 3to2 -w <filepath>
-w, --write           Write back modified files
-n, --nobackups       Donot write backups for modified files.
# 转换整个文件夹下所有文件
$ 3to2 -w <dirpath>

py-backwards

https://github.com/nvbn/py-backwards

安装

$ pip install py-backwards

使用

$ py-backwards -i src -o compiled -t 2.7
$ py-backwards -i input.py -o output.py -t 2.7

2转3

2to3

自动将 Python 2 代码转为 Python 3 代码

2to3适用 修复器 来转换代码,其支持库lib2to3,

注释和缩进都会在转换过程中保持不变。

使用

转换前

def greet(name):
    print "Hello, {0}!".format(name)
print "What's your name?"
name = raw_input()
greet(name)

转换命令

# 预览转换
$ 2to3 example.py
# 覆盖原文件
$ 2to3 -w example.py
$ 2to3 -nw example.py  # 不创建源文件副本

转换后

def greet(name):
    print("Hello, {0}!".format(name))
print("What's your name?")
name = input()
greet(name)

参考文档

}!".format(name))
print(“What’s your name?”)
name = input()
greet(name)


# 参考文档

[nvbn](https://github.com/nvbn) /  **py-backwards**   https://github.com/nvbn/py-backwards
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值