Python3의 2to3 변환 도구 사용 예
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을 추가하면 된다.백은 있는 게 좋을 거야.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.