Python3中的2to3转换工具使用示例
python3与python2的还是有诸多的不同,比如说在2中:
print "Hello,World!"
raw_input()
在3里面就成了:
print ("Hello,World!")
input()
所以如果用的python2开发的项目要迁移到3中,就需要进行代码的转换。Python3中自带了个转换工具,下面用个最简单的例子来说说2to3转换工具。
例子:(2to3Test.py 里面只有print这行代码)
# python 2.7.6
# 2to3Test.py
print "Hello,World!"
用python27显然是可以编译的:
D:\Python>python27 2to3Test.py
Hello,World!
用python33就编译不过了,因为3里print是函数,这样写就会有语法错误。
D:\Python>python33 2to3Test.py
File "2to3Test.py", line 1
print "Hello,World!"
^
SyntaxError: invalid syntax
下面用python3中自带的2to3工具进行转换:
D:\Python>python C:\Python33\Tools\Scripts\2to3.py -w 2to3Test.py
RefactoringTool: Skipping implicit fixer: buffer
RefactoringTool: Skipping implicit fixer: idioms
RefactoringTool: Skipping implicit fixer: set_literal
RefactoringTool: Skipping implicit fixer: ws_comma
RefactoringTool: Refactored 2to3Test.py
--- 2to3Test.py (original)
+++ 2to3Test.py (refactored)
@@ -1 +1 @@
-print "Hello,World!"
+print("Hello,World!")
RefactoringTool: Files that were modified:
RefactoringTool: 2to3Test.py
最后用python33来进行编译,结果显示正确的。
D:\Python>python33 2to3Test.py
Hello,World!
总结:
1. 目录. C:\Python33\Tools\Scripts\2to3.py. 其实在python2.6,2.7中都存在这个工具。
2. 如果不加-w参数,则默认只是把转换过程所对应的diff内容打印输出到当前窗口而已。
3. 加了-w,就是把改动内容,写回到原先的文件了。
4. 不想要生成bak文件,再加上-n即可。 bak最好还是有。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Overview of how to use Python's join() function to merge strings: In Python, we often encounter situations where we need to merge multiple strings. Python provides a very convenient method join() to merge strings. This article will introduce how to use the join() function and provide some specific code examples. How to use the join() function: The join() function is a method of string. It accepts an iterable object as a parameter and adds the elements in the object to the

Introduction to Python functions: functions and usage examples of the globals function Python is a powerful programming language that provides many built-in functions, among which the globals() function is one of them. This article will introduce the functions and usage examples of the globals() function, with specific code examples. 1. Functions of the globals function The globals() function is a built-in function that returns a dictionary of global variables of the current module. It returns a dictionary containing global variables, where

Introduction to Python functions: functions and usage examples of float functions. Python is a high-level programming language widely used in many fields. It provides a wealth of built-in functions so that developers can develop and process data more conveniently. One of them is the float function, which is used to convert strings or numbers into floating point types. In this article, we will introduce the functions of the float function in detail and give some usage examples. Introduction to the function of float function: float function in Python

Introduction to Python functions: functions and usage examples of the vars function In Python programming, vars() is a very useful built-in function that returns a dictionary of attributes and values of an object. This function can be used to obtain all properties and corresponding values of an object, including variables, functions, classes, modules, etc. The vars() function can accept one parameter, which is an object. If no parameters are passed in, the vars() function will return a dictionary of all global variables in the current scope. And if passed in

Introduction to Python functions: functions and usage examples of locals functions Python is a programming language widely used in various fields. Its powerful function features help programmers organize and manage code effectively. In Python, there are many built-in functions that can help us complete programming tasks better. One very useful function is locals(). This article will introduce the functions and usage examples of the locals function in detail, and provide specific code examples. 1. Function of locals function loc

Accurate and reliable dedecms conversion tool evaluation report With the rapid development of the Internet era, website construction has become one of the necessary tools for many companies and individuals. In website construction, using a content management system (CMS) can manage website content and functions more conveniently and efficiently. Among them, dedecms, as a well-known CMS system, is widely used in various website construction projects. However, sometimes we are faced with the need to convert the dedecms website to other formats, in which case we need to use a conversion tool

Introduction to Python functions: functions and usage examples of the compile function. In Python programming, the compile() function is a built-in function. Its function is to compile Python code in the form of a string into bytecode or AST objects. After compilation, the code can be run by executing the bytecode or AST object. In this article, we will provide a detailed introduction to the function and use of the compile function and provide some practical code examples. Syntax and parameters of compile function

Installation steps: 1. Make sure that Python3 has been installed and can be accessed through the command line; 2. Open the terminal and enter the "python3 -m ensurepip --upgrade" command to install pip; 3. Download the pip installation package from the official Python website; 4. Extract the downloaded pip installation package into a directory; 5. Open the terminal and navigate to the decompressed pip directory; 6. Run the "python3 setup.py install" command to install pip.
