【发布时间】:2017-07-25 11:55:04
【问题描述】:
我想把Python2代码翻译成Python 3,很简单,但是不行
import sys
import MySQLdb
import Cookbook
try:
conn = Cookbook.connect ()
print "Connected"
except MySQLdb.Error, e:
print "Cannot connect to server"
print "Error code:", e.args[0]
print "Error message:", e.args[1]
sys.exit (1)
conn.close ()
print "Disconnected"
我在终端得到了这个
2to3 harness.py
RefactoringTool: Skipping optional fixer: buffer
RefactoringTool: Skipping optional fixer: idioms
RefactoringTool: Skipping optional fixer: set_literal
RefactoringTool: Skipping optional fixer: ws_comma
RefactoringTool: Can't parse harness.py: ParseError: bad input: type=1, value='print', context=('', (9, 0))
RefactoringTool: No files need to be modified.
RefactoringTool: There was 1 error:
RefactoringTool: Can't parse harness.py: ParseError: bad input: type=1, value='print', context=('', (9, 0))
为什么?
【问题讨论】:
-
这是您原始文件的缩进还是在此处发布时弄乱了?
-
原文件缩进。
-
我已更改为制表符缩进,但还是一样。
-
缩进本身就被完全修饰了,在 Python 中 标识就是一切。这就是它失败的原因。
-
如果你的程序不能正常工作只是因为缩进不当,那么你的问题本身就是错误的
标签: python python-2to3