Python 2.x to 3.x - Append Syntaxerror: how to make it compatibale with Python 3.x from 2.x?
Python 2.x to 3.x - Append Syntaxerror: how to make it compatibale with Python 3.x from 2.x?
我有一个非常简单的问题,但我很难找到 - 这意味着一个非常简单的答案。在过去 ~12 年左右的时间里,我有很多人在 python 2x 中编写的大量脚本,我不得不移植到 python 3x。
经过搜索和许多很多修复后,我 运行 进行了一个难以追踪的更改。我在 e.append(lineno, line
) 中有语法错误,如下所示。关于如何改变它以使其 python 3x 兼容的任何想法都将非常有用。
self.__options = {}
lineno = 0
e = None # None, or an exception
while 1:
line = fp.readline()
if not line:
break
lineno = lineno + 1
# skip blank lines
if string.strip(line) == '':
continue
# skip lines starting with '['
if line[0] == '[':
continue
# remove anything after '#'
line = string.split(line, '#')[0]
# key/value pairs can be seperated by whitespaces
for opt in string.split(line):
#if opt in string.whitespace:
# continue
keyval = string.split(opt, '=')
if len(keyval) == 2:
self.__options[keyval[0]] = keyval[1]
else:
e = ParsingError(fpname)
e.append(lineno, `line`)
# if any parsing errors occurred, raise an exception
if e:
raise e
这是非常简洁的历史语法。
对于反引号,我建议您参考这个答案:. The gist is that backticks are shorthand for calling repr
until they were removed in python 3. They are mentioned in the docs 作为“转换”操作:
repr
(object)
Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function...
虽然 undocumented use of the ParsingError.append
方法:
,但追加本身是正常的
def append(self, lineno, line):
从版本 3 开始,ConfigParser
had been renamed configparser
, but still has the ParsingError.append
方法。
因此您需要进行一些更改:
文件开头,将from ConfigParser import ParsingError
改为
从 configparser 导入 ParsingError
把错误的行改成
e.append(lineno, repr(line))
备注
append
的多个参数引起了 list.append
自 python 2.0 以来不受支持的行为,但与之无关。您可以在此处找到注释:https://docs.python.org/2/library/stdtypes.html#mutable-sequence-types。具体脚注:
- The C implementation of Python has historically accepted multiple parameters and implicitly joined them into a tuple; this no longer works in Python 2.0. Use of this misfeature has been deprecated since Python 1.4.
我有一个非常简单的问题,但我很难找到 - 这意味着一个非常简单的答案。在过去 ~12 年左右的时间里,我有很多人在 python 2x 中编写的大量脚本,我不得不移植到 python 3x。
经过搜索和许多很多修复后,我 运行 进行了一个难以追踪的更改。我在 e.append(lineno, line
) 中有语法错误,如下所示。关于如何改变它以使其 python 3x 兼容的任何想法都将非常有用。
self.__options = {}
lineno = 0
e = None # None, or an exception
while 1:
line = fp.readline()
if not line:
break
lineno = lineno + 1
# skip blank lines
if string.strip(line) == '':
continue
# skip lines starting with '['
if line[0] == '[':
continue
# remove anything after '#'
line = string.split(line, '#')[0]
# key/value pairs can be seperated by whitespaces
for opt in string.split(line):
#if opt in string.whitespace:
# continue
keyval = string.split(opt, '=')
if len(keyval) == 2:
self.__options[keyval[0]] = keyval[1]
else:
e = ParsingError(fpname)
e.append(lineno, `line`)
# if any parsing errors occurred, raise an exception
if e:
raise e
这是非常简洁的历史语法。
对于反引号,我建议您参考这个答案:. The gist is that backticks are shorthand for calling repr
until they were removed in python 3. They are mentioned in the docs 作为“转换”操作:
repr
(object)Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function...
虽然 undocumented use of the ParsingError.append
方法:
def append(self, lineno, line):
从版本 3 开始,ConfigParser
had been renamed configparser
, but still has the ParsingError.append
方法。
因此您需要进行一些更改:
文件开头,将
from ConfigParser import ParsingError
改为从 configparser 导入 ParsingError
把错误的行改成
e.append(lineno, repr(line))
备注
append
的多个参数引起了 list.append
自 python 2.0 以来不受支持的行为,但与之无关。您可以在此处找到注释:https://docs.python.org/2/library/stdtypes.html#mutable-sequence-types。具体脚注:
- The C implementation of Python has historically accepted multiple parameters and implicitly joined them into a tuple; this no longer works in Python 2.0. Use of this misfeature has been deprecated since Python 1.4.