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

歡迎訪問 生活随笔!

生活随笔

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

python

python中parse是什么_python中的configparse学习笔记

發布時間:2024/1/23 python 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中parse是什么_python中的configparse学习笔记 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

configparse主要用于在python中進行配置文件的讀取。

基本的讀取配置文件:

-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() 函數

-write(filename,'w')) 保存配置cf.write(open(filename,'w'))

基本的寫入配置文件:

-add_section(section) 添加一個新的section

-set( section, option, value) 對section中的option進行設置,需要調用write將內容寫入配置文件

實例如下:

配置文件:

[xiong]

name=xinog

age= 23gender=male

[ying]

name=ying

age= 24gender= female

python腳本:

#encoding:utf-8

from configparser importConfigParser

cf=ConfigParser(allow_no_value=True)

cf.read('my.ini')print(cf.sections())#獲取配置文件中的sections,結果:['xiong', 'ying']

print(cf.has_section('xiong'))#是否有xiong這個section,結果:True

print(cf.items('xiong'))#返回xiong這個section中的子項,結果:[('name', 'xinog'), ('age', '23'), ('gender', 'male')]

print(cf.options('xiong'))#返回xiong這個section中的變量,結果:['name', 'age', 'gender']

print(cf.has_option('xiong','age'))#判斷xiong這個section中是否有age這本變量

print(cf.get('xiong','age'))#獲取xiong這個section中age變量的值#cf.remove_section('xiong')#移除xiong這個section

cf.add_section("cai")#添加一個叫cai的section

cf.set('cai','host','127.0.0.1')#保存文件

cf.write(open('my.ini','w'))

被代碼更新后的配置文件:

[xiong]

name=xinog

age= 23gender=male

[ying]

name=ying

age= 24gender=female

[cai]

host= 127.0.0.1

如果需要更詳細點,請參考:https://www.cnblogs.com/louhui/p/9089444.html#_label0

總結

以上是生活随笔為你收集整理的python中parse是什么_python中的configparse学习笔记的全部內容,希望文章能夠幫你解決所遇到的問題。

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