Reputation: 364
Is there a way in the Package "ConfigParser" to obtain the line-number in which the Parser had read a specific section or key?
[section1]
option1=3
option2=4
[section2]
option3=right
i.e. a method that returns the line-number for section1 (lineNo 1) or for option3 (lineNo 5).
Upvotes: 3
Views: 993
Reputation: 27073
As far as I know python's ConfigParser does not keep track of line numbers.
You can convince yourself by reading the source code. Here the link to the source code of ConfigParser
from python 2.7.9: https://hg.python.org/cpython/file/648dcafa7e5f/Lib/ConfigParser.py
And the source code in python 3.4 (the most current commit at the time of writing): https://hg.python.org/cpython/file/516d3773ecb2/Lib/configparser.py
Reading a file is done by either read()
or readfp()
. Both are just wrappers around _read()
. The method _read()
does keep track of the line number in the variable lineno
but as far as I can tell lineno
is only used to report error.
Here is an overview of alternative config parsers: https://wiki.python.org/moin/ConfigParserShootout
From a quick search it seems that only INITools keeps track of line numbers.
I do not know how current the information in the wiki is.
Upvotes: 3