6

.iniファイルにこのようなものがあります

[General]
verbosity = 3   ; inline comment

[Valid Area Codes]
; Input records will be checked to make sure they begin with one of the area 
; codes listed below.  

02    ; Central East New South Wales & Australian Capital Territory
03    ; South East Victoria & Tasmania
;04    ; Mobile Telephones Australia-wide
07    ; North East Queensland
08    ; Central & West Western Australia, South Australia & Northern Territory

ただし、インラインコメントがkey = value行で機能しているがkey、値のない行では機能していないという問題があります。ConfigParserオブジェクトを作成する方法は次のとおりです。

>>> import ConfigParser
>>> c = ConfigParser.SafeConfigParser(allow_no_value=True)
>>> c.read('example.ini')
['example.ini']
>>> c.get('General', 'verbosity')
'3'
>>> c.options('General')
['verbosity']
>>> c.options('Valid Area Codes')
['02    ; central east new south wales & australian capital territory', '03    ; south east victoria & tasmania', '07    ; north east queensland', '08    ; central & west western australia, south australia & northern territory']

インラインコメントが両方の場合に機能するように構成パーサーを設定するにはどうすればよいですか?

4

3 に答える 3

10

ConfigParserのドキュメントによると

"構成ファイルには、特定の文字 (# および ;) を先頭に付けたコメントを含めることができます。コメントは、それ以外の場合は空の行に単独で表示されるか、値またはセクション名を保持する行に入力することができます"

あなたの場合、値のないキーだけを保持する行にコメントを追加しています(したがって、機能しません)。そのため、その出力が得られます。

参照: http://docs.python.org/library/configparser.html#safeconfigparser-objects

于 2012-02-29T05:55:32.640 に答える
0

02= ; comment代わりに試してみてください。

于 2012-02-29T01:49:38.090 に答える