创新互联Python教程:tomllib—-ParseTOMLfiles

tomllib —- Parse TOML files

3.11 新版功能.

Source code: Lib/tomllib


This module provides an interface for parsing TOML (Tom’s Obvious Minimal Language, https://toml.io). This module does not support writing TOML.

参见

The Tomli-W package is a TOML writer that can be used in conjunction with this module, providing a write API familiar to users of the standard library marshal and pickle modules.

参见

The TOML Kit package is a style-preserving TOML library with both read and write capability. It is a recommended replacement for this module for editing already existing TOML files.

This module defines the following functions:

tomllib.load(fp, /, **, parse_float=float*)

Read a TOML file. The first argument should be a readable and binary file object. Return a dict. Convert TOML types to python using this conversion table.

parse_float will be called with the string of every TOML float to be decoded. By default, this is equivalent to float(num_str). This can be used to use another datatype or parser for TOML floats (e.g. decimal.Decimal). The callable must not return a dict or a list, else a ValueError is raised.

A TOMLDecodeError will be raised on an invalid TOML document.

tomllib.loads(s, /, **, parse_float=float*)

Load TOML from a str object. Return a dict. Convert TOML types to Python using this conversion table. The parse_float argument has the same meaning as in load().

A TOMLDecodeError will be raised on an invalid TOML document.

The following exceptions are available:

exception tomllib.TOMLDecodeError

Subclass of ValueError.

Examples

Parsing a TOML file:

 
 
 
 
  1. import tomllib
  2. with open("pyproject.toml", "rb") as f:
  3. data = tomllib.load(f)

Parsing a TOML string:

 
 
 
 
  1. import tomllib
  2. toml_str = """
  3. python-version = "3.11.0"
  4. python-implementation = "CPython"
  5. """
  6. data = tomllib.loads(toml_str)

Conversion Table

TOML

Python

table

dict

string

str

integer

int

float

float (configurable with parse_float)

boolean

bool

offset date-time

datetime.datetime (tzinfo attribute set to an instance of datetime.timezone)

local date-time

datetime.datetime (tzinfo attribute set to None)

local date

datetime.date

local time

datetime.time

array

list

当前标题:创新互联Python教程:tomllib—-ParseTOMLfiles
标题来源:http://www.shufengxianlan.com/qtweb/news11/353311.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联