Python configparser 模塊

      網友投稿 707 2022-05-29

      該模塊適用于配置文件的格式與windows ini文件類似,可以包含一個或多個節(section),每個節可以有多個參數(鍵=值)。

      創建文件

      來看一個好多軟件的常見文檔格式如下:

      [DEFAULT] ServerAliveInterval?=?45 Compression?=?yes CompressionLevel?=?9 ForwardX11?=?yes ?? [bitbucket.org] User?=?hg ?? [topsecret.server.com] Port?=?50022 ForwardX11?=?no

      如果想用python生成一個這樣的文檔怎么做呢?

      import?configparser config?=?configparser.ConfigParser() config["DEFAULT"]?=?{'ServerAliveInterval':?'45', ?????????????????????'Compression':?'yes', ?????????????????????'CompressionLevel':?'9', ?????????????????????'ForwardX11':?'yes' ?????????????????????} config['bitbucket.org']?=?{'User':?'hg'} config['topsecret.server.com']?=?{'Host?Port':?'50022',?'ForwardX11':?'no'} with?open("config.ini",?"w",?encoding="utf-8")?as?configfile: ????config.write(configfile) configfile.close()

      結果:

      [DEFAULT] serveraliveinterval?=?45 compression?=?yes compressionlevel?=?9 forwardx11?=?yes [bitbucket.org] user?=?hg [topsecret.server.com] host?port?=?50022 forwardx11?=?no

      查找文件

      import?configparser config?=?configparser.ConfigParser() #?---------------------------查找文件內容,基于字典的形式 print(config.sections()) config.read("config.ini") print(config.sections()) print("bytebong.com"?in?config) print("bitbucket.org"?in?config) print(config["bitbucket.org"]["user"]) print(config["DEFAULT"]["Compression"]) print(config["topsecret.server.com"]["Forwardx11"]) print(config["bitbucket.org"]) for?key?in?config:??#?注意,有default會默認default的鍵 ????print(key) print(config.options("bitbucket.org"))??#?同for循環,找到'bitbucket.org'下所有鍵 print(config.items("bitbucket.org"))??#?找到'bitbucket.org'下所有鍵值對 print(config.get("bitbucket.org",?'compression'))??#?get方法Section下的key對應的value

      結果:

      D:\YuchuanProjectData\PythonProject\venv\Scripts\python.exe?D:/YuchuanProjectData/PythonProject/YuchuanDemo010.py [] ['bitbucket.org',?'topsecret.server.com'] False True hg yes no DEFAULT bitbucket.org topsecret.server.com ['user',?'serveraliveinterval',?'compression',?'compressionlevel',?'forwardx11'] [('serveraliveinterval',?'45'),?('compression',?'yes'),?('compressionlevel',?'9'),?('forwardx11',?'yes'),?('user',?'hg')] yes Process?finished?with?exit?code?0

      增刪改操作

      Python configparser 模塊

      import?configparser config?=?configparser.ConfigParser() config.read("config.ini") config.add_section("yuchuan") config.remove_section("bitbucket.org") config.remove_option("topsecret.server.com",?"forwardx11") config.set("topsecret.server.com",?"k2",?"22222") config.set("yuchuan",?"kk",?"666666") config.write(open("new.ini",?"w",?encoding="utf-8"))

      結果:

      [DEFAULT] serveraliveinterval?=?45 compression?=?yes compressionlevel?=?9 forwardx11?=?yes [topsecret.server.com] host?port?=?50022 k2?=?22222 [yuchuan] kk?=?666666

      軟件開發 人工智能 機器學習 AI

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:臺達杯自動化大賽設計方案
      下一篇:Python 爬蟲進階二之 PySpider 框架安裝配置
      相關文章
      亚洲午夜爱爱香蕉片| 亚洲熟妇无码八V在线播放| 亚洲第一视频网站| 亚洲国产av玩弄放荡人妇| 亚洲精品综合在线影院| 亚洲另类无码专区丝袜| 亚洲国产精品白丝在线观看| 自拍偷自拍亚洲精品播放| 亚洲人成人网站18禁| 亚洲国产成人久久精品99| 亚洲国产成人精品电影| 亚洲色无码专区一区| 亚洲毛片αv无线播放一区| 亚洲国产美国国产综合一区二区| 99久久亚洲精品无码毛片| 亚洲中文字幕无码永久在线| www国产亚洲精品久久久日本| 国产亚洲精品观看91在线| 亚洲国产主播精品极品网红| 亚洲AV日韩AV天堂一区二区三区| 亚洲精品人成网在线播放影院| 一本色道久久88亚洲精品综合| 亚洲精品成人久久久| 亚洲av成人一区二区三区在线观看 | 久久久亚洲AV波多野结衣| 国产精品亚洲一区二区三区久久 | 国产亚洲精品无码成人| 最新国产AV无码专区亚洲| 亚洲经典在线中文字幕| 亚洲一区二区三区电影| jlzzjlzz亚洲乱熟在线播放| 亚洲性无码一区二区三区| 亚洲成a人片在线观看老师| 亚洲日韩人妻第一页| 亚洲人成网网址在线看| 亚洲国产一区二区a毛片| 亚洲国产综合无码一区二区二三区| 中文字幕亚洲日本岛国片| 亚洲国产高清在线| 亚洲免费人成视频观看| 亚洲AV无码欧洲AV无码网站|