# python 2.x
# from ConfigParser import SafeConfigParser
# config = SafeConfigParser()
# python 3.x
from configparser import ConfigParser
config = ConfigParser()
config.read('config.ini')
config.add_section('main')
config.set('main', 'key1', 'value1')
config.set('main', 'key2', 'value2')
config.set('main', 'key3', 'value3')
with open('config.ini', 'w') as f:
config.write(f)
文件格式非常简单,各节用方括号标出:
[main]
key1 = value1
key2 = value2
key3 = value3
可以像下面这样从文件中提取值:
# python 2.x
# from ConfigParser import SafeConfigParser
# config = SafeConfigParser()
# python 3.x
from configparser import ConfigParser
config = ConfigParser()
config.read('config.ini')
print(config.get('main', 'key1')) # -> "value1"
print(config.get('main', 'key2')) # -> "value2"
print(config.get('main', 'key3')) # -> "value3"
# getfloat() raises an exception if the value is not a float
a_float = config.getfloat('main', 'a_float')
# getint() and getboolean() also do this for their respective types
an_int = config.getint('main', 'an_int')
JSON [ . JSON 格式]
JSON 数据可能非常复杂,并且具有高度可移植性的优点。
将数据写入文件:
import json
config = {"key1": "value1", "key2": "value2"}
with open('config1.json', 'w') as f:
json.dump(config, f)
从文件中读取数据:
import json
with open('config.json', 'r') as f:
config = json.load(f)
#edit the data
config['key3'] = 'value3'
#write it back to the file
with open('config.json', 'w') as f:
json.dump(config, f)
#!/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 基本示例
Python 社区似乎根本不用于配置文件。然而,解析/编写 XML 很容易,而且使用 Python 也有很多这样做的可能性。其中之一是美味汤:
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)
from readsettings import ReadSettings
data = ReadSettings("settings.json") # Load or create any json, yml, yaml or toml file
data["name"] = "value" # Set "name" to "value"
data["name"] # Returns: "value"
import json
class Dict(dict):
"""dot.notation access to dictionary attributes"""
__getattr__ = dict.__getitem__
__setattr__ = dict.__setitem__
__delattr__ = dict.__delitem__
class Config(object):
@staticmethod
def __load__(data):
if type(data) is dict:
return Config.load_dict(data)
elif type(data) is list:
return Config.load_list(data)
else:
return data
@staticmethod
def load_dict(data: dict):
result = Dict()
for key, value in data.items():
result[key] = Config.__load__(value)
return result
@staticmethod
def load_list(data: list):
result = [Config.__load__(item) for item in data]
return result
@staticmethod
def load_json(path: str):
with open(path, "r") as f:
result = Config.__load__(json.loads(f.read()))
return result
import json
class Configurator:
def __init__(self):
# Hard coded values if config file doesn't exist
self.alpha: int = 42
self.bravo: float = 3.14
self.charlie: str = "8.8.8.8"
self.delta: list = ["Lorem", "ipsum", "dolor", "sit", "amet"]
self.echo: dict = {"Winter": "is coming"}
def read_config_file(self, config_file_name: str = "config.json"):
try:
with open(config_file_name) as conf_file:
for k, v in json.loads(conf_file.read()).items():
setattr(self, k, v)
except Exception as e:
print(f"Error was detected while reading {config_file_name}: {str(e)}. Hard coded values will be applied")
def save_config_file(self, config_file_name: str = "config.json"):
try:
conf_items = {k: v for k, v in vars(self).items() if isinstance(v, (int, float, str, list, dict))}
with open(config_file_name, "w") as conf_file:
json.dump(conf_items, conf_file, sort_keys=False, indent=2)
except Exception as e:
print(f"Error was detected while saving {config_file_name}: {str(e)}")
from configurator import Configurator
if __name__ == '__main__':
conf = Configurator()
# Read config (values from file or hard coded values if file doesn't exist)
conf.read_config_file()
# Using values from config
a = conf.alpha
# Changing values in config
conf.bravo += 1
# Save changed config to file
conf.save_config_file()
如果配置文件不存在,它将出现在第一次调用 Save _ config _ file ()之后。如果在此之后更改 config.json,则文件中的变量必须在下一次“击败”硬编码变量。