[英]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. 假设我有一个名为sample.ini的配置文件,那么其中有两个部分。
[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. 现在,如果我要打印Url_3和Url_4,Python中是否有办法只能打印这两个Url。
I tried looking about this, but they provide solution which print every section contents in config file. 我试着看一下,但是他们提供了在配置文件中打印每个部分内容的解决方案。
Please help me with that. 请帮我。
Could you try giving this a shot? 您可以试一下吗? Using python's config parser
使用python的配置解析器
sample.ini 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
You can use configobj: 您可以使用configobj:
Sample.ini 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: 您可以像这样
walk
文件部分和键:
sample.ini 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
或只使用字典清单,而不用担心
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'}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.