toml
1 toml 例子
[owner]
name = "Tom Preston-Werner"
organization = "Github"
bio = "Github Cofounder & CEO\nLikes tater tots and beer."
dob = 1979-05-27T07:32:00Z # 日期时间是一等公民.为什么不呢?
[database]
server = "192.168.1.1"
ports = [ 8001, 8001, 8002 ]
connection_max = 5000
enabled = true
[servers]
# 您可以依照您的意愿缩进.使用空格或Tab.TOML不会在意.
[servers.alpha]
ip = "10.0.0.1"
dc = "eqdc10"
[servers.beta]
ip = "10.0.0.2"
dc = "eqdc10"
[clients]
data = [ ["gamma", "delta"], [1, 2] ]
解析为 dict
{
u'owner': {
u'dob': datetime.datetime(1979, 5, 27, 7, 32, tzinfo=<xlib.util.toml.tz.TomlTz object at 0x7f2e2ba706d0>),
u'organization': u'Github',
u'name': u'Tom Preston-Werner',
u'bio': u'Github Cofounder & CEO\nLikes tater tots and beer.'
},
u'servers': {
u'alpha': {u'ip': u'10.0.0.1', u'dc': u'eqdc10'},
u'beta': {u'ip': u'10.0.0.2', u'dc': u'eqdc10'}
},
u'clients': {
u'data': [[u'gamma', u'delta'], [1, 2]]
},
u'database': {
u'enabled': True,
u'ports': [8001, 8001, 8002],
u'connection_max': 5000,
u'server': u'192.168.1.1'
}
}
2 使用
from xlib.util import toml
# Parse a file or a list of files as TOML and return a dictionary.
toml.load(f, _dict=dict)
# Parse a TOML-formatted string to a dictionary.
toml.loads(s, _dict=dict)
# Write a dictionary to a file containing TOML-formatted data
toml.dump(o, f, encoder=None)
# Create a TOML-formatted string from an input object
toml.dumps(o, encoder=None)
# eg:
with open("./blb.toml", 'r') as f:
toml_config = toml.load(f)
print toml_config
with open("./blb2.toml", 'w') as f:
toml.dump(toml_config, f)
2.1 toml list
[[user_list]]
UserID = "user1"
[[user_list]]
UserID = "user2"
[[user_list]]
UserID = "user3"
转为 json
{
"user_list": [
{
"UserID": "user1"
},
{
"UserID": "user2"
},
{
"UserID": "user3"
}
]
}
Last updated