简体   繁体   English

在读取多个配置文件时,ConfigParser会覆盖以前的文件,为什么?

[英]When reading multiple config files, ConfigParser overwrites previous files, why?

So I wanted to start by saying I have been looking through out SO for an answer to this, and haven't been able to find anything useful. 所以我想首先说我一直在寻找SO的答案,并且找不到任何有用的东西。 I've also looked through Python's docs and failed to find something useful. 我也查看了Python的文档,但未能找到有用的东西。 My last question was slightly lazy and received negative feedback, so I'm doing everything I can to ask a simple and straightforward question here. 我的上一个问题有点懒,收到负面反馈,所以我尽我所能在这里提出一个简单明了的问题。 As always, thanks in advance for any help! 一如既往,提前感谢您的帮助!

So, can someone please explain this odd behavior that I am experiencing with Python's ConfigParser. 那么,有人可以解释我在Python的ConfigParser中遇到的奇怪行为。 I have two different configuration files, each with a Section 1 . 我有两个不同的配置文件,每个文件都有一个Section 1 The two files have differing numbers of options, but the one with a lesser number of options is overwritten. 这两个文件具有不同数量的选项,但覆盖的选项数量较少。 Here is the code and the output: 这是代码和输出:

First Config File: test1.ini 第一个配置文件:test1.ini

[Section 1]
Option 1 : One
Option 2 : Two
Option 3 : None
Option 4 : Four

Second Config File: test2.ini 第二个配置文件:test2.ini

[Section 1]
Option 1 : One
Option 2 : None
Option 3 : Three

Driver that reads config files 读取配置文件的驱动程序

from ConfigParser import SafeConfigParser

parser = SafeConfigParser()

def ParseThis(file, section):
    parser.read(file)

    for option in parser.options(section):
        print "\t" + option
        try:
            if parser.get(section, option) != 'None':
                print option + ": " + parser.get(section, option)
            else:
                print option + ": Option doesn't exist"
        except:
            print option + ": Something went wrong"

print "First File:"
print "Section 1"
ParseThis('test2.ini', 'Section 1')


print "\n"
print "Second File: "
print "Section 1"
ParseThis('test1.ini', 'Section 1')

print "\n"
print "First File: "
print "Section 1"
ParseThis('test2.ini', 'Section 1')

And Here is the output that I get, which makes no sense. 这是我得到的输出,这没有任何意义。

First File:
Section 1
    option 1
option 1: One
    option 2
option 2: Option doesn't exist
    option 3
option 3: Three


Second File: 
Section 1
    option 1
option 1: One
    option 2
option 2: Two
    option 3
option 3: Option doesn't exist
    option 4
option 4: Four


First File: 
Section 1
    option 1
option 1: One
    option 2
option 2: Option doesn't exist
    option 3
option 3: Three
    option 4   <== why this line?
option 4: Four <== why this line?

A single ConfigParser instance is meant to represent a single configuration, which may be derived from multiple files in a "precedence order" such that later files override earlier ones. 单个ConfigParser实例用于表示单个配置,该配置可以以“优先顺序”从多个文件派生,以便后面的文件覆盖之前的文件。 The documentation does not make this totally clear, but does say: 文档没有说明这一点,但确实说:

This is designed so that you can specify a list of potential configuration file locations (for example, the current directory, the user's home directory, and some system-wide directory), and all existing configuration files in the list will be read. 其设计使您可以指定潜在配置文件位置的列表(例如,当前目录,用户的主目录和某些系统范围的目录),并且将读取列表中的所有现有配置文件。

If you want the configurations you read to be kept separate, you need to create a separate SafeConfigParser instance for each one. 如果您希望将读取的配置保持独立,则需要为每个配置创建单独的SafeConfigParser实例。 Move your parser = SafeConfigParser() line inside the function. 在函数内移动parser = SafeConfigParser()行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM