ConfigParser मूल उदाहरण
फ़ाइल को इस तरह लोड और इस्तेमाल किया जा सकता है:
#!/usr/bin/env python
import ConfigParser
import io
# Load the configuration file
with open("config.yml") as f:
sample_config = f.read()
config = ConfigParser.RawConfigParser(allow_no_value=True)
config.readfp(io.BytesIO(sample_config))
# List all contents
print("List all contents")
for section in config.sections():
print("Section: %s" % section)
for options in config.options(section):
print("x %s:::%s:::%s" % (options,
config.get(section, options),
str(type(options))))
# Print some contents
print("\nPrint some contents")
print(config.get('other', 'use_anonymous')) # Just get the value
print(config.getboolean('other', 'use_anonymous')) # You know the datatype?
जो आउटपुट देता है
List all contents
Section: mysql
x host:::localhost:::<type 'str'>
x user:::root:::<type 'str'>
x passwd:::my secret password:::<type 'str'>
x db:::write-math:::<type 'str'>
Section: other
x preprocessing_queue:::["preprocessing.scale_and_center",
"preprocessing.dot_reduction",
"preprocessing.connect_lines"]:::<type 'str'>
x use_anonymous:::yes:::<type 'str'>
Print some contents
yes
True
जैसा कि आप देख सकते हैं, आप एक मानक डेटा प्रारूप का उपयोग कर सकते हैं जो पढ़ने और लिखने में आसान है। गेटबुलियन और गेटिंट जैसे तरीके आपको एक साधारण स्ट्रिंग के बजाय डेटाटाइप प्राप्त करने की अनुमति देते हैं।
लेखन विन्यास
import os
configfile_name = "config.yaml"
# Check if there is already a configurtion file
if not os.path.isfile(configfile_name):
# Create the configuration file as it doesn't exist yet
cfgfile = open(configfile_name, 'w')
# Add content to the file
Config = ConfigParser.ConfigParser()
Config.add_section('mysql')
Config.set('mysql', 'host', 'localhost')
Config.set('mysql', 'user', 'root')
Config.set('mysql', 'passwd', 'my secret password')
Config.set('mysql', 'db', 'write-math')
Config.add_section('other')
Config.set('other',
'preprocessing_queue',
['preprocessing.scale_and_center',
'preprocessing.dot_reduction',
'preprocessing.connect_lines'])
Config.set('other', 'use_anonymous', True)
Config.write(cfgfile)
cfgfile.close()
का परिणाम
[mysql]
host = localhost
user = root
passwd = my secret password
db = write-math
[other]
preprocessing_queue = ['preprocessing.scale_and_center', 'preprocessing.dot_reduction', 'preprocessing.connect_lines']
use_anonymous = True
XML मूल उदाहरण
लगता है कि पायथन समुदाय द्वारा कॉन्फ़िगरेशन फ़ाइलों के लिए बिल्कुल भी उपयोग नहीं किया जाएगा। हालाँकि, XML को पार्स करना / लिखना आसान है और पायथन के साथ ऐसा करने की काफी संभावनाएं हैं। एक सुंदर है:
from BeautifulSoup import BeautifulSoup
with open("config.xml") as f:
content = f.read()
y = BeautifulSoup(content)
print(y.mysql.host.contents[0])
for tag in y.other.preprocessing_queue:
print(tag)
जहाँ config.xml इस तरह दिख सकता है
<config>
<mysql>
<host>localhost</host>
<user>root</user>
<passwd>my secret password</passwd>
<db>write-math</db>
</mysql>
<other>
<preprocessing_queue>
<li>preprocessing.scale_and_center</li>
<li>preprocessing.dot_reduction</li>
<li>preprocessing.connect_lines</li>
</preprocessing_queue>
<use_anonymous value="true" />
</other>
</config>
.ini
है किconfigparser
मॉड्यूल के समान प्रारूप का उपयोग करना चाहिए जो आपको चाहिए।