# 示例代码:使用 Python 读取和写入 TOML 文件
# 首先需要安装 toml 包,可以使用 pip 安装:
# pip install toml
import toml
# 写入 TOML 文件
data = {
'title': 'TOML Example',
'owner': {
'name': 'Tom Preston-Werner',
'dob': '1979-05-27T07:32:00Z'
},
'database': {
'server': '192.168.1.1',
'ports': [8001, 8002, 8003],
'connection_max': 5000,
'enabled': True
}
}
with open('config.toml', 'w') as f:
toml.dump(data, f)
# 读取 TOML 文件
with open('config.toml', 'r') as f:
config = toml.load(f)
print(config)
toml 包:首先需要通过 pip install toml 安装 toml 包。toml.dump() 方法将 Python 字典写入到 TOML 文件中。toml.load() 方法从 TOML 文件中读取数据并转换为 Python 字典。这段代码展示了如何使用 Python 处理 TOML 文件的基本操作。
上一篇:python request库
下一篇:python升级命令
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站