python讀取配置 python讀取配置檔案

2021-10-18 15:49:52 字數 1279 閱讀 4982

本機:python3.5

1.需要安裝configerparser 【py2是 configparser】

2.編寫**

執行前#coding:utf8

import configparser

conf = configparser.configparser()

conf.read("f:\\python\\clstudydemo\\util\\config.ini")

# 獲取指定的section,指定的option的值

url = conf.get("global","url")

print (url)

#獲取所有的sections

all = conf.sections()

print(all)

#寫配置檔案

# 更新指定section,option的值

conf.set("global","url","")

#新增新的option值

conf.set("global","classname","通訊")

# 新增新的 section

conf.add_section("new_section")

conf.set("new_section", "new_option", "")

# 寫回配置檔案

conf.write(open("f:\\python\\clstudydemo\\util\\config.ini","w"))

執行後

基本的讀取配置檔案

-read(filename) 直接讀取ini檔案內容

-sections() 得到所有的section,並以列表的形式返回

-options(section) 得到該section的所有option

-items(section) 得到該section的所有鍵值對

-get(section,option) 得到section中option的值,返回為string型別

-getint(section,option) 得到section中option的值,返回為int型別,還有相應的getboolean()和getfloat() 函式。

基本的寫入配置檔案

-add_section(section) 新增乙個新的section

-set( section, option, value) 對section中的option進行設定,需要呼叫write將內容寫入配置檔案。

python讀取properties配置檔案並解析

ld.properties 配置資訊 database.type argo database.name dd database.ip 10.12.15.225 database.port 10000 database.username user database.password passwd pr...

Python 配置檔案讀取

python提供了configparser包來進行配置檔案讀取。api 所謂的sections就是乙個配置塊。下面是乙個配置檔案db.ini mongo db host 127.0.0.1 db port 27017 system timeout 2000 interval 2000 包含mongo...

python讀取配置檔案

在對pyqt的使用中,很多變數是需要靈活配置的,防止動輒需要修改程式。python提供了configparser模組 配置檔案片段如下 db 資料庫型別 db type mysql,oracle,highgo 片段如下 configfile component.ini cf configparser...