日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python Configparser模块读取、写入配置文件

發布時間:2023/11/29 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python Configparser模块读取、写入配置文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

寫代碼中需要用到讀取配置,最近在寫python,記錄一下。

如下,假設有這樣的配置。

[db] db_host=127.0.0.1 db_port=3306 db_user=root db_pass= [concurrent] thread=200 processor=400

可以使用ConfigParser模塊來讀取、寫入配置。

1 #coding=utf-8 2 import ConfigParser 3 import sys 4 5 cf = ConfigParser.ConfigParser() 6 cf.read(sys.argv[1]) 7 8 # 返回所有的section 9 s = cf.sections() 10 print s #['db', 'concurrent'] 11 12 # 返回db下面所有的options 13 db_options = cf.options("db") 14 print db_options #['db_host', 'db_port', 'db_user', 'db_pass'] 15 16 print cf.get("db", "db_host") #127.0.0.1 17 print cf.getint("db", "db_port") #3306 18 19 # 修改一個值, 并寫回去 20 cf.set("db", "db_host", "losthost") 21 # 添加一個section 22 cf.add_section("new_concurrent") 23 cf.set("new_concurrent", "thread", "500") 24 25 ##刪除一個section 26 cf.remove_section('concurrent') 27 cf.write(open("test_new.conf", "w")) 28 #cf.write(open(sys.argv[1], "w"))

完.

轉載于:https://www.cnblogs.com/xudong-bupt/p/7434967.html

總結

以上是生活随笔為你收集整理的Python Configparser模块读取、写入配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。