您好,登錄后才能下訂單哦!
這篇文章主要介紹Python中讀寫配置文件的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
讀文件
代碼如下:
from configobj import ConfigObj config = ConfigObj(filename) # value1 = config['keyword1'] value2 = config['keyword2'] # section1 = config['section1'] value3 = section1['keyword3'] value4 = section1['keyword4'] # # you could also write value3 = config['section1']['keyword3'] value4 = config['section1']['keyword4']
寫文件
代碼如下:
from configobj import ConfigObj config = ConfigObj() config.filename = filename # config['keyword1'] = value1 config['keyword2'] = value2 # config['section1'] = {} config['section1']['keyword3'] = value3 config['section1']['keyword4'] = value4 # section2 = { 'keyword5': value5, 'keyword6': value6, 'sub-section': { 'keyword7': value7 } } config['section2'] = section2 # config['section3'] = {} config['section3']['keyword 8'] = [value8, value9, value10] config['section3']['keyword 9'] = [value11, value12, value13] # config.write()
以上是Python中讀寫配置文件的方法的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。