打印 Python 中配置文件特定部分的内容

Printing contents in particular section of configuration file in Python

假设我有一个名为 sample.ini 的配置文件,让其中有两个部分。

    [section1]
    Name1 = Url_1
    Name2 = Url_2

    [Section2]
    Name3 = Url_3
    Name4 = Url_4

现在如果我想打印 Url_3 和 Url_4,在 Python 中有没有办法让我只能打印这两个 Url.

我试着查看这个,但他们提供了打印配置文件中每个部分内容的解决方案。

请帮助我。

你能试试这个吗?使用 python 的配置解析器

sample.ini

[section1]
Name1=Url_1
Name2=Url_2

[Section2]
Name3=Url_3
Name4=Url_4

脚本:

import ConfigParser as configparser

parser = configparser.ConfigParser()
parser.read("sample.ini")

section_2 = dict(parser.items("Section2")) 
print section_2["name3"]
print section_2["name4"]

输出:

Url_3
Url_4

您可以使用 configobj:
Sample.ini

Name=Top level
[section1]
Name1=Url_1
Name2=Url_2

[Section2]
Name3=Url_3
Name4=Url_4

[Section3]
    [[SubSection]]
    Name3=Url_3
    Name4=Url_4

代码:

from configobj import ConfigObj
cfg = ConfigObj("sample.ini")
print cfg["Name"]
print cfg["Section2"]["Name3"]
print cfg["Section2"]["Name4"]
print cfg["Section2"]
print cfg["Section3"]["SubSection"]["Name3"]

输出:

Top level
Url_3
Url_4
{'Name3': 'Url_3', 'Name4': 'Url_4'}
Url_3

编辑: 我想这可能就是您通过动态访问所指的内容。
您可以 walk 文件部分和密钥,如下所示:

sample.ini

Name=Top level
[section1]
Name1=Url_1
Name2=Url_2

[Section2]
Name3=Url_3
Name4=Url_4

[Section3]
    [[SubSection]]
    Name5=Url_5
    Name6=Url_6

代码:

from configobj import ConfigObj
cfg = ConfigObj("sample.ini")
#
def transform(section, key):
    val = section[key]
    print val
print "\nPrint Keys and Values"
cfg.walk(transform, call_on_sections=True)
print "\nPrint Values only"
cfg.walk(transform, call_on_sections=False)
print "\nContents of sample.ini\n"
print cfg
print "\nHunt just for Url5"
def transform2(section, key):
    val = section[key]
    if val == "Url_5":
        print val, "is in ", section
cfg.walk(transform2, call_on_sections=True)
print "\nList dictionary"
for key in cfg:
    print "%s: %s" % (key, cfg[key])

结果:

Print Keys and Values
Top level
{'Name1': 'Url_1', 'Name2': 'Url_2'}
Url_1
Url_2
{'Name3': 'Url_3', 'Name4': 'Url_4'}
Url_3
Url_4
{'SubSection': {'Name5': 'Url_5', 'Name6': 'Url_6'}}
{'Name5': 'Url_5', 'Name6': 'Url_6'}
Url_5
Url_6

Print Values only
Top level
Url_1
Url_2
Url_3
Url_4
Url_5
Url_6

Contents of sample.ini

{'Name': 'Top level', 'section1': {'Name1': 'Url_1', 'Name2': 'Url_2'}, 'Section2': {'Name3': 'Url_3', 'Name4': 'Url_4'}, 'Section3': {'SubSection': {'Name5': 'Url_5', 'Name6': 'Url_6'}}}

Hunt just for Url5
Url_5 is in  {'Name5': 'Url_5', 'Name6': 'Url_6'}

List dictionary
Name: Top level
section1: {'Name1': 'Url_1', 'Name2': 'Url_2'}
Section2: {'Name3': 'Url_3', 'Name4': 'Url_4'}
Section3: {'SubSection': {'Name5': 'Url_5', 'Name6': 'Url_6'}}

或者只使用字典列表,不要理会 walk

from configobj import ConfigObj
cfg = ConfigObj("sample.ini")
for key in cfg:
    print "%s: %s" % (key, cfg[key])

结果:

Name: Top level
section1: {'Name1': 'Url_1', 'Name2': 'Url_2'}
Section2: {'Name3': 'Url_3', 'Name4': 'Url_4'}
Section3: {'SubSection': {'Name5': 'Url_5', 'Name6': 'Url_6'}}