Python?configparser模塊的用法示例代碼
1 configparser 使用get方式讀取.ini配置文件的配置內(nèi)容
(1)首先編寫如下所示的env.ini配置文件
[server] ip=192.168.1.200 port=22 username=root password=root [personal] name=redrose2100 city=nanjing github=redrose2100.github.io
(2) 編寫解析.ini配置文件的python代碼
import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") print(config.get("server","ip")) print(config.get("personal","name"))
運行結(jié)果為:
192.168.1.200
redrose2100
2 使用數(shù)組下標的方式讀取.ini配置文件的內(nèi)容
env.ini的內(nèi)容同上述1中的內(nèi)容
通過數(shù)組下標的方式讀取配置文件內(nèi)容的代碼如下:
import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") print(config["server"]["ip"]) print(config["personal"]["name"])
執(zhí)行結(jié)果如下:
192.168.1.200
redrose2100
3 使用configparser寫配置文件
import configparser config=configparser.ConfigParser() config["server"]={ "ip":"192.138.1.200", "port":22, "username":"root", "password":"root" } config["personal"]={ "name":"redrose2100", "city":"nanjing" } with open("test.ini","w") as f: config.write(f)
執(zhí)行之后,在當前目錄下會生成一個test.ini文件,其內(nèi)容如下:
[server] ip = 192.138.1.200 port = 22 username = root password = root [personal] name = redrose2100 city = nanjing
4 configparser 對section常用的操作:
- (1)has_section(section) 判斷讀取的config對象是否還有指定的section
- (2)sections() 獲取讀取到的config對象的所有sections列表
- (3)add_section(section) 給讀取到的config對象增加一個section,注意此時增加的section只是在config對象中,并沒有寫入到ini配置文件中
- (4)remove_section(section) 給讀取到的config對象刪除一個section
實例代碼如下所示:
import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") print(config.has_section("server")) print(config.sections()) config.add_section("kafka") print(config.sections()) config.remove_section("kafka") print(config.sections())
運行結(jié)果如下:
import configparser
config=configparser.ConfigParser()
config.read("env.ini","utf-8")
print(config.has_section("server"))
print(config.sections())
config.add_section("kafka")
print(config.sections())
config.remove_section("kafka")
print(config.sections())
5 configparser對option常用的操作,如下代碼演示:
import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") print(config.has_option("server","ip")) print(config.options("server")) config.set("server","test","test") print(config.options("server")) config.remove_option("server","test") print(config.options("server"))
執(zhí)行結(jié)果為:
True
['ip', 'port', 'username', 'password']
['ip', 'port', 'username', 'password', 'test']
['ip', 'port', 'username', 'password']
6 configparser的對象可以類似字典一樣使用,但是類型不是字典,代碼演示如下:
import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") for key in config["server"].keys(): print(key) for key,value in config["server"].items(): print(key,value) for value in config["server"].values(): print(value) item=config["server"].popitem() print(type(item)) print(item) port=config["server"].pop("port") print(port) for key,value in config["server"].items(): print(key,value) username=config["server"].get("username","no found") print(username) print(type(config["server"]))
運行結(jié)果如下:
ip
port
username
password
ip 192.168.1.200
port 22
username root
password root
192.168.1.200
22
root
root
<class 'tuple'>
('ip', '192.168.1.200')
22
username root
password root
root
<class 'configparser.SectionProxy'>
7 可以將獲取的類型直接轉(zhuǎn)換為期望的數(shù)據(jù)類型,可用的方法有:
- getint
- getboolean
- getfloat
- get
下面將配置文件更新如下內(nèi)容:
[server] ip=192.168.1.200 port=22 username=root password=root is_linux=True price=100.24 [personal] name=redrose2100 city=nanjing github=redrose2100.github.io
實例代碼如下:
import configparser config=configparser.ConfigParser() config.read("env.ini","utf-8") ip=config["server"].get("ip") port=config["server"].getint("port") is_linux=config["server"].getboolean("is_linux") price=config["server"].getfloat("price") print(ip,type(ip)) print(port,type(port)) print(is_linux,type(is_linux)) print(price,type(price))
運行結(jié)果如下:
192.168.1.200 <class 'str'>
22 <class 'int'>
True <class 'bool'>
100.24 <class 'float'>
8 configparser標準庫對解析.conf文件與解析.ini文件的使用方法是完全一樣的,下面只演示一部分:
創(chuàng)建一個env.conf文件,內(nèi)容如下:
[server] ip=192.168.1.200 port=22 username=root password=root is_linux=True price=100.24 [personal] name=redrose2100 city=nanjing github=redrose2100.github.io
編寫如下代碼:
import configparser config=configparser.ConfigParser() config.read("env.conf","utf-8") print(config.get("server","ip")) print(config.get("personal","name")) print(config["server"]["ip"]) print(config["personal"]["name"])
運行結(jié)果如下:
192.168.1.200
redrose2100
192.168.1.200
redrose2100
到此這篇關(guān)于Python configparser模塊的用法的文章就介紹到這了,更多相關(guān)Python configparser模塊內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Python中Parser的超詳細用法實例
- python中parser.add_argument()用法實例(命令行選項、參數(shù)和子命令解析器)
- Python Parser的用法
- Python ArgumentParse的subparser用法說明
- Python3中configparser模塊讀寫ini文件并解析配置的用法詳解
- Python3.5內(nèi)置模塊之shelve模塊、xml模塊、configparser模塊、hashlib、hmac模塊用法分析
- Python HTML解析模塊HTMLParser用法分析【爬蟲工具】
- Python中optparser庫用法實例詳解
- python命令行參數(shù)解析OptionParser類用法實例
- Python中Parser的用法小結(jié)
相關(guān)文章
PyTorch中Tensor的數(shù)據(jù)統(tǒng)計示例
今天小編就為大家分享一篇PyTorch中Tensor的數(shù)據(jù)統(tǒng)計示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02Pytorch GPU內(nèi)存占用很高,但是利用率很低如何解決
這篇文章主要介紹了Pytorch GPU內(nèi)存占用很高,但是利用率很低的原因及解決方法,具有很好的參考價值,希望對大家 有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06徹底吃透理解Python基礎(chǔ)33個關(guān)鍵字詳細教程
這篇文章主要為大家介紹了徹底吃透理解Python中33個關(guān)鍵字的詳細教程,有需要打好Python基礎(chǔ)的同學(xué)可以借鑒參考下,希望能成為您成功路上的一塊墊腳石2021-10-10python實現(xiàn)爬蟲抓取小說功能示例【抓取金庸小說】
這篇文章主要介紹了python實現(xiàn)爬蟲抓取小說功能,結(jié)合具體實例形式分析了使用Python爬蟲抓取金庸小說的具體操作技巧,需要的朋友可以參考下2019-08-08Ubuntu 下 vim 搭建python 環(huán)境 配置
這篇文章主要介紹了Ubuntu 下 vim 搭建python環(huán)境配置,需要的朋友可以參考下2017-06-06