簡體   English   中英

Python中如何編輯保存TOML內容

[英]How to edit and save TOML content in Python

我想編輯一個本地 TOML 文件並再次保存,以便在同一個 Python 腳本中使用。 從這個意義上講,能夠在循環中更改給定的參數。 您可以在此處查看文件示例。

https://bitbucket.org/robmoss/particle-filter-for-python/src/master/src/pypfilt/examples/predation.toml

到目前為止,我可以加載文件,但我找不到如何更改參數值。

import toml
data = toml.load("scenario.toml")

使用 toml.load 讀取文件后,您可以修改數據,然后使用toml.dump命令覆蓋所有內容

import toml
data = toml.load("scenario.toml") 

# Modify field
data['component']['model']='NEWMODELNAME' # Generic item from example you posted

# To use the dump function, you need to open the file in 'write' mode
# It did not work if I just specify file location like in load
f = open("scenario.toml",'w')
toml.dump(data, f)
f.close()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM