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

歡迎訪問 生活随笔!

生活随笔

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

python

python configparser 注释_使用configpar添加注释

發布時間:2023/12/1 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python configparser 注释_使用configpar添加注释 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如果您想去掉尾隨的=,可以按照atomopter的建議將ConfigParser.ConfigParser子類化,并實現自己的write方法來替換原來的方法:import sys

import ConfigParser

class ConfigParserWithComments(ConfigParser.ConfigParser):

def add_comment(self, section, comment):

self.set(section, '; %s' % (comment,), None)

def write(self, fp):

"""Write an .ini-format representation of the configuration state."""

if self._defaults:

fp.write("[%s]\n" % ConfigParser.DEFAULTSECT)

for (key, value) in self._defaults.items():

self._write_item(fp, key, value)

fp.write("\n")

for section in self._sections:

fp.write("[%s]\n" % section)

for (key, value) in self._sections[section].items():

self._write_item(fp, key, value)

fp.write("\n")

def _write_item(self, fp, key, value):

if key.startswith(';') and value is None:

fp.write("%s\n" % (key,))

else:

fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))

config = ConfigParserWithComments()

config.add_section('Section')

config.set('Section', 'key', 'value')

config.add_comment('Section', 'this is the comment')

config.write(sys.stdout)

此腳本的輸出為:

^{pr2}$

注意事項:如果使用名稱以;開頭且值設置為None的選項名,則該選項將被視為注釋。在

這將允許您添加注釋并將其寫入文件,但不會將其讀回。為此,您將實現自己的_read方法,該方法負責解析注釋,并可能添加一個comments方法來獲得每個部分的注釋。在

總結

以上是生活随笔為你收集整理的python configparser 注释_使用configpar添加注释的全部內容,希望文章能夠幫你解決所遇到的問題。

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