【问题标题】:Automated Python translation 2to3 error自动 Python 翻译 2to3 错误
【发布时间】: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


【解决方案1】:

不知道这是否能解决您的问题,但您可以尝试修复缩进:

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"

【讨论】:

    【解决方案2】:

    我认为问题可能出在 MySQLdb 上。此软件包仅支持最高 2.7,不支持 python 3。目前 MySQL-python 1.2.5 是最新版本(25-07-2017)

    MySQL 版本 3.23 到 5.5 和 Python-2.4 到 2.7 目前是 支持的。未来的版本将支持 Python-3.0。 PyPy 是 支持。

    【讨论】:

    • 不,我可以从 python shell 使用 MySQLdb。
    • 这些行打印的内容 print "Error code:", e.args[0] print "Error message:", e.args[1]
    【解决方案3】:

    这就是 2to3 所做的

    2to3 new.py
    RefactoringTool: Skipping optional fixer: buffer
    RefactoringTool: Skipping optional fixer: idioms
    RefactoringTool: Skipping optional fixer: set_literal
    RefactoringTool: Skipping optional fixer: ws_comma
    RefactoringTool: Refactored new.py
    --- new.py  (original)
    +++ new.py  (refactored)
    @@ -4,12 +4,12 @@
    
     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]
    +    print("Connected")
    +except MySQLdb.Error as 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"
    +print("Disconnected")
    RefactoringTool: Files that need to be modified:
    RefactoringTool: new.py
    

    我又变了

    except MySQLdb.Error as e:
    

    现在我有了 Python3 代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-27
      • 2013-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多