2015-10-05 82 views
0

让我们想象一下,我有一个名称为sample.ini的配置文件,让它有两个部分。在Python中配置文件的特定部分的打印内容

[section1] 
    Name1 = Url_1 
    Name2 = Url_2 

    [Section2] 
    Name3 = Url_3 
    Name4 = Url_4 

现在,如果我想打印Url_3 & Url_4,有没有在Python的方式,我只能打印这两个网址。

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

请帮助我。

回答

0

你可以试试这个镜头吗?使用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 
+0

感谢您的解决方案,但我们可以用for循环中的动态任何部分内容的访问? – RakshiKn

+0

'parser.sections'将为您提供所有可用的部分。你可以循环通过,可能。 – sihrc

+0

谢谢你的帮助..将检查.. – RakshiKn

0

您可以使用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'}} 
+0

谢谢你的帮助。但不是手动给Name3或Name4,它可以在代码中动态地完成吗? – RakshiKn

+0

例如?我假设你必须知道你在配置文件中寻找什么,所以我不清楚它是什么意思的动态。 –

+0

请参阅已编辑的“动态”访问模拟答案 –