用于Tom显而易见的最小语言的Python库
toml的Python项目详细描述
用于解析和创建TOML的python库。
模块通过the TOML test suite。
另请参见:
安装
要在PyPI上安装最新版本, 只需运行:
pip install toml
或者要安装最新的开发版本,请运行:
git clone https://github.com/uiri/toml.git cd toml python setup.py install
快速教程
toml.loads接受包含标准toml格式数据和 返回包含已分析数据的字典。
>>> importtoml>>> toml_string=""" ... # This is a TOML document. ...... title = "TOML Example" ...... [owner] ... name = "Tom Preston-Werner" ... dob = 1979-05-27T07:32:00-08:00 # First class dates ...... [database] ... server = "192.168.1.1" ... ports = [ 8001, 8001, 8002 ] ... connection_max = 5000 ... enabled = true ...... [servers] ...... # Indentation (tabs and/or spaces) is allowed but not required ... [servers.alpha] ... ip = "10.0.0.1" ... dc = "eqdc10" ...... [servers.beta] ... ip = "10.0.0.2" ... dc = "eqdc10" ...... [clients] ... data = [ ["gamma", "delta"], [1, 2] ] ...... # Line breaks are OK when inside arrays ... hosts = [ ... "alpha", ... "omega" ... ] ... """>>> parsed_toml=toml.loads(toml_string)
toml.dumps接受字典并返回包含 相应的toml格式数据。
>>> new_toml_string=toml.dumps(parsed_toml)>>> print(new_toml_string)title = "TOML Example" [owner] name = "Tom Preston-Werner" dob = 1979-05-27T07:32:00Z [database] server = "192.168.1.1" ports = [ 8001, 8001, 8002,] connection_max = 5000 enabled = true [clients] data = [ [ "gamma", "delta",], [ 1, 2,],] hosts = [ "alpha", "omega",] [servers.alpha] ip = "10.0.0.1" dc = "eqdc10" [servers.beta] ip = "10.0.0.2" dc = "eqdc10"
有关更多函数,请查看下面的api引用。
API参考
- toml.load(f, _dict=dict)
将文件或文件列表解析为toml并返回字典。
Args: - ^{tt2}$: A path to a file, list of filepaths (to be read into single object) or a file descriptor
- ^{tt3}$: The class of the dictionary object to be returned
Returns: A dictionary (or object ^{tt3}$) containing parsed TOML data
Raises: - ^{tt5}$: When ^{tt2}$ is an invalid type or is a list containing invalid types
- ^{tt7}$: When an error occurs while decoding the file(s)
- toml.loads(s, _dict=dict)
将toml格式的字符串解析为字典。
Args: - ^{tt9}$: The TOML-formatted string to be parsed
- ^{tt3}$: Specifies the class of the returned toml dictionary
Returns: A dictionary (or object ^{tt3}$) containing parsed TOML data
Raises: - ^{tt5}$: When a non-string object is passed
- ^{tt7}$: When an error occurs while decoding the TOML-formatted string
- toml.dump(o, f)
将字典写入包含toml格式数据的文件
Args: - ^{tt15}$: An object to be converted into TOML
- ^{tt2}$: A File descriptor where the TOML-formatted output should be stored
Returns: A string containing the TOML-formatted data corresponding to object ^{tt15}$
Raises: - ^{tt5}$: When anything other than file descriptor is passed
- toml.dumps(o)
从输入对象创建一个toml格式的字符串
Args: - ^{tt15}$: An object to be converted into TOML
Returns: A string containing the TOML-formatted data corresponding to object ^{tt15}$
许可
这个项目是根据麻省理工学院开放源码许可证的条款发布的。查看 license.txt了解更多信息。