jjzjj

ConfigParser

全部标签

python - 关闭由 ConfigParser 打开的文件

我有以下内容:config=ConfigParser()config.read('connections.cfg')sections=config.sections()如何关闭使用config.read打开的文件?在我的例子中,当新的部分/数据被添加到config.cfg文件时,我更新了我的wxtree小部件。但是,它只更新一次,我怀疑这是因为config.read使文件保持打开状态。当我们讨论它时,ConfigParser和RawConfigParser之间的主要区别是什么? 最佳答案 ConfigParser.read(file

python - 如何 ConfigParse 文件为相同的键保留多个值?

我需要能够使用ConfigParser读取同一键的多个值。示例配置文件:[test]foo=value1foo=value2xxx=yyy通过ConfigParser的“标准”使用,将有一个键foo,其值为value2。但我需要解析器读取这两个值。跟随entryonduplicatekey我创建了以下示例代码:fromcollectionsimportOrderedDictfromConfigParserimportRawConfigParserclassOrderedMultisetDict(OrderedDict):def__setitem__(self,key,value):tr

python - ConfigParser 和带有环境变量的字符串插值

我有点不懂python语法,我在读取带有内插值的.ini文件时遇到问题。这是我的ini文件:[DEFAULT]home=$HOMEtest_home=$home[test]test_1=$test_home/foo.csvtest_2=$test_home/bar.csv那些线fromConfigParserimportSafeConfigParserparser=SafeConfigParser()parser.read('config.ini')printparser.get('test','test_1')是否输出$test_home/foo.csv在我怀孕的时候/Users/n

Python ConfigParser.NoSectionError : No section:

我不断收到ConfigParser.NoSectionError:Nosection:'file'错误我的配置文件如下:[file]api_access_id=123445567api_default_org=NAMEapi_secret_key=kjvhkd28unfkjsapi_base_url=www.fakeurl.com/api我的代码如下:config=ConfigParser.RawConfigParser()configFilePath='E:\Python\configfile\test.txt'config.read(configFilePath)try:api_a

Python ConfigParser - 引号之间的值

当使用ConfigParser模块时,我想使用包含在cfg文件中设置的多个单词的值。在这种情况下,用(example.cfg):这样的引号将字符串括起来对我来说似乎微不足道[GENERAL]onekey="valueinsomewords"我的问题是,在这种情况下,当使用这样的值时,python也会将引号附加到字符串中:config=ConfigParser()config.read(["example.cfg"])printconfig.get('GENERAL','onekey')我确信有一个内置功能可以设法只打印'valueinsomewords'而不是'"valueinsome

python - 如何使用已在 ConfigParser 中定义的变量

我在Python中使用ConfigParserconfig.ini是[general]name:my_namebase_dir:/home/myhome/expexe_dir:${base_dir}/bin我希望exp_dir成为/home/myhome/exp/bin而不是${base_dir}/bin。这意味着${base_dir}将自动替换为/home/myhome/exp。 最佳答案 您可以使用ConfigParser插值Ontopofthecorefunctionality,SafeConfigParsersupports

python - 如何从 Python ConfigParser .items() 中排除 DEFAULT?

我正在使用ConfigParser从配置文件加载数据,如下所示:测试.conf:[myfiles]fileone:%(datadir)s/somefile.foofiletwo:%(datadir)s/nudderfile.foo加载.py:importConfigParserconfig=ConfigParser.ConfigParser({'datadir':'/tmp'})config.read('test.conf')printconfig.items('myfiles')printconfig.get('myfiles','datadir')输出:$pythonload.py

python - 如何从 Python ConfigParser .items() 中排除 DEFAULT?

我正在使用ConfigParser从配置文件加载数据,如下所示:测试.conf:[myfiles]fileone:%(datadir)s/somefile.foofiletwo:%(datadir)s/nudderfile.foo加载.py:importConfigParserconfig=ConfigParser.ConfigParser({'datadir':'/tmp'})config.read('test.conf')printconfig.items('myfiles')printconfig.get('myfiles','datadir')输出:$pythonload.py

python - 用Python修改INI文件

我有一个需要使用Python修改的INI文件。我正在查看ConfigParser模块,但仍然遇到问题。我的代码是这样的:config=ConfigParser.RawConfigParser()config.read('C:\itb\itb\Webcams\AMCap1\amcap.ini')config.set('Video','Path','C:\itb\itb')但是在运行这段代码后查看amcap.ini文件时,它仍然没有被修改。谁能告诉我我做错了什么? 最佳答案 ConfigParser不会自动写回磁盘上的文件。使用.wri

python - 用Python修改INI文件

我有一个需要使用Python修改的INI文件。我正在查看ConfigParser模块,但仍然遇到问题。我的代码是这样的:config=ConfigParser.RawConfigParser()config.read('C:\itb\itb\Webcams\AMCap1\amcap.ini')config.set('Video','Path','C:\itb\itb')但是在运行这段代码后查看amcap.ini文件时,它仍然没有被修改。谁能告诉我我做错了什么? 最佳答案 ConfigParser不会自动写回磁盘上的文件。使用.wri