您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

将代码从Python 2.x转换为3.x

将代码从Python 2.x转换为3.x

LexError后删除逗号。可以在Python 2和Python 3中使用。

在Python 2中,很少使用语法来引发如下异常:

raise ExceptionClass, "The message string"

这是在这里使用的,但是由于某种原因,也许由于消息字符串周围有一个括号(根据Senthils测试,是括号中的换行符),2to3错过了更好的更改:

raise ExceptionClass("The message string")

因此它应该看起来像这样(在Python 2中)

message = "%s:%d: Rule '%s' returned an unkNown token type '%s'" % (
           func.func_code.co_filename, func.func_code.co_firstlineno,
           func.__name__, newtok.type),lexdata[lexpos:])
raise LexError(message)

因为在与加薪相同的行上格式化该消息很困难。:-)然后,func_code被重命名,因此在Python 3中有更多更改。但是通过上述更改,2to3应该可以正常工作。

python 2022/1/1 18:36:33 有413人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶