Python:使用2to3将Python2转Python3

简介: Python:使用2to3将Python2转Python3

Python2中的print是一个语句,而Python3中是一个函数

Python2代码 example.py

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

命令行中执行指令

$ 2to3 -w example.py

Python3代码

def greet(name):

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

书写兼容代码

from future import print_function

参考:

https://docs.python.org/2/library/2to3.html

            </div>
目录
相关文章
|
28天前
|
Python
619: 蟠桃记(python)
619: 蟠桃记(python)
|
9天前
|
并行计算 数据库 数据库管理
Python中starmap有什么用的?
Python中starmap有什么用的?
15 6
|
6月前
|
JSON NoSQL Redis
Python使用小结
Python使用小结
40 0
|
8月前
|
Python
python的这些小知识你注意到了吗?
本文将对一些小知识进行汇总,这些知识也许你用了 python 很久,但仍然没注意到。
|
12月前
|
Python
Python生日蛋糕
Hello,小伙伴们晚上好吖!前两天博主满20岁啦(要开始奔三辽呜呜呜),这几天收到了不少小伙伴们的祝福,浪漫的小博主想送给大家一份不一样的生日蛋糕,感谢大家对俺滴支持!
108 0
|
Python
Python:使用2to3将Python2转Python3
Python:使用2to3将Python2转Python3
53 0
|
SQL Java 关系型数据库
数据持久化技术(Python)的使用
- 传统数据库连接方式:mysql(PyMySQL) - ORM 模型:SQLAlchemy MyBatis、 Hibernate ## PyMySQL 安装: ``` pip install pymysql ``` ## 简单使用 利用 pymysql.connect 建立数据库连接并执行 SQL 命令(需要提前搭建好数据库): ``` import pymysql db =
|
Python 数据采集
python HTML解析器
一般的爬虫解析 html 用  sgmlib 或者 lxml 解析  lxml 解析速度是 BS 的 20 倍以上 http://www.
793 0