[英]Python 2.x to 3.x - Append Syntaxerror: how to make it compatibale with Python 3.x from 2.x?
I have a very simple question that I am having difficulty tracing down - which implies a very simple answer.我有一个非常简单的问题,我很难追查 - 这意味着一个非常简单的答案。 I have a slew of scripts written by a slew of people over the last ~12 years or so in python 2x that I am having to port over to python 3x.
在过去约 12 年左右的时间里,我在 python 2x 中编写了大量脚本,我不得不移植到 python 3x。
After searching and many, many, many fixes I have run into a change that is difficult to track down.经过搜索和许多、许多、许多修复后,我遇到了一个难以追踪的更改。 I have a syntax error in e.append(lineno,
line
) shown below.我在如下所示的 e.append(lineno,
line
) 中有语法错误。 Any ideas on how to alter this to make it python 3x compatible would be super useful.关于如何改变它以使其与 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
This is pretty neat historical syntax.这是非常简洁的历史语法。
For backticks, I refer you to this answer: https://stackoverflow.com/a/1673087/2988730 .对于反引号,我建议您参考以下答案: https://stackoverflow.com/a/1673087/2988730 。 The gist is that backticks are shorthand for calling
repr
until they were removed in python 3. They are mentioned in the docs as the "conversion" operation:要点是反引号是调用
repr
的简写,直到它们在 python 3 中被删除。它们在文档中被称为“转换”操作:
repr
( object )代表(object
repr
Return a string containing a printable representation of an object.
返回包含 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...
有时能够像普通的 function 一样访问此操作很有用...
The append itself is a normal, though undocumented use of the ParsingError.append
method: append 本身是正常的,尽管没有记录使用
ParsingError.append
方法:
def append(self, lineno, line):
Since version 3, ConfigParser
had been renamed configparser
, but still has the ParsingError.append
method.从版本 3 开始,
ConfigParser
已重命名为configparser
,但仍然具有ParsingError.append
方法。
So you need a couple of changes:因此,您需要进行一些更改:
At the beginning of the file, change from ConfigParser import ParsingError
to在文件的开头,将
from ConfigParser import ParsingError
更改为
from configparser import ParsingError从 configparser 导入 ParsingError
Change the line with the error to将有错误的行更改为
e.append(lineno, repr(line))
Note笔记
The multiple arguments to append
evoke, but aren't related to, a behavior of list.append
that hasn't been supported since python 2.0. The multiple arguments to
append
evoke, but aren't related to, a behavior of list.append
that hasn't been supported since python 2.0. You can find a note here: https://docs.python.org/2/library/stdtypes.html#mutable-sequence-types .您可以在此处找到注释: https://docs.python.org/2/library/stdtypes.html#mutable-sequence-types 。 Specifically the footnote:
特别是脚注:
- The C implementation of Python has historically accepted multiple parameters and implicitly joined them into a tuple;
Python 的 C 实现历来接受多个参数并将它们隐式连接到一个元组中; this no longer works in Python 2.0.
这不再适用于 Python 2.0。 Use of this misfeature has been deprecated since Python 1.4.
自 Python 1.4 起,已弃用此错误功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.