【发布时间】:2009-02-19 10:27:21
【问题描述】:
基于 ConfigParser 模块如何过滤并抛出 ini 文件中的每个 cmets?
import ConfigParser
config = ConfigParser.ConfigParser()
config.read("sample.cfg")
for section in config.sections():
print section
for option in config.options(section):
print option, "=", config.get(section, option)
例如。在上面的基本脚本下面的 ini 文件中,打印出进一步的 cmets 行,例如:
something = 128 ; comment line1
; further comments
; one more line comment
我需要的是里面只有部分名称和纯键值对,没有任何 cmets。 ConfigParser 是否以某种方式处理这个问题,或者我应该使用正则表达式......还是?干杯
【问题讨论】:
-
“扔掉”是什么意思?请明确说明您真正想要做什么——为什么需要从文件中“丢弃”数据?它去哪儿了?留下了什么?
标签: python configparser