RakshiKn
RakshiKn

Reputation: 45

Printing contents in particular section of configuration file in Python

Lets imagine i have an config file by name sample.ini, let there be two section in that.

    [section1]
    Name1 = Url_1
    Name2 = Url_2

    [Section2]
    Name3 = Url_3
    Name4 = Url_4

Now if I want to print Url_3 & Url_4, is there a way in Python that I can only print those two Url.

I tried looking about this, but they provide solution which print every section contents in config file.

Please help me with that.

Upvotes: 0

Views: 3133

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

You can use 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

Code:

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"]

Output:

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

EDIT: I suppose that this might be what you are referring to by access dynamically.
You can walk the files sections and keys like so:

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

Code:

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])

Result:

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'}}

OR just use the dictionary listing and don't bother with the walk

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

Result:

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'}}

Upvotes: 0

sihrc
sihrc

Reputation: 2828

Could you try giving this a shot? Using python's config parser

sample.ini

[section1]
Name1=Url_1
Name2=Url_2

[Section2]
Name3=Url_3
Name4=Url_4

Script:

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"]

Output:

Url_3
Url_4

Upvotes: 1

Related Questions