python修改ini保留注释_保留注释换行的python模块configparser

def_read(self, fp, fpname):"""Parse a sectioned setup file.

The sections in setup file contains a title line at the top,

indicated by a name in square brackets (`[]'), plus key/value

options lines, indicated by `name: value' format lines.

Continuations are represented by an embedded newline then

leading whitespace. Blank lines, lines beginning with a '#',

and just about everything else are ignored."""cursect= None #None, or a dictionary

optname =None

lineno=0

e= None #None, or an exception

comment_line_cache = [] #comment or blank line temp cache

whileTrue:

line=fp.readline()if notline:breaklineno= lineno + 1

#comment or blank line?

if line.strip() == '' or line[0] in '#;':

comment_line_cache.append(line.strip())continue

if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":#no leading whitespace

comment_line_cache.append(line.strip())continue

#continuation line?

if line[0].isspace() and cursect is not None andoptname:

value=line.strip()ifvalue:

cursect[optname].append(value)#a section header or option header?

else:#is it a section header?

mo =self.SECTCRE.match(line)ifmo:

sectname= mo.group('header')

self.comment_line_dict[sectname]=comment_line_cache

comment_line_cache=[]if sectname inself._sections:

cursect=self._sections[sectname]elif sectname ==DEFAULTSECT:

cursect=self._defaultselse:

cursect=self._dict()

cursect['__name__'] =sectname

self._sections[sectname]=cursect#So sections can't start with a continuation line

optname =None#no section header in the file?

elif cursect isNone:raiseMissingSectionHeaderError(fpname, lineno, line)#an option line?

else:

mo=self._optcre.match(line)ifmo:

optname, vi, optval= mo.group('option', 'vi', 'value')

optname=self.optionxform(optname.rstrip())

self.comment_line_dict["%s.%s"%(cursect['__name__'], optname)] =comment_line_cache

comment_line_cache=[]#This check is fine because the OPTCRE cannot

#match if it would set optval to None

if optval is notNone:if vi in ('=', ':') and ';' inoptval:#';' is a comment delimiter only if it follows

#a spacing character

pos = optval.find(';')if pos != -1 and optval[pos-1].isspace():

optval=optval[:pos]

optval=optval.strip()#allow empty values

if optval == '""':

optval= ''cursect[optname]=[optval]else:#valueless option handling

cursect[optname] =optvalelse:#a non-fatal parsing error occurred. set up the

#exception but keep going. the exception will be

#raised at the end of the file and will contain a

#list of all bogus lines

if note:

e=ParsingError(fpname)

e.append(lineno, repr(line))#if any parsing errors occurred, raise an exception

ife:raisee#join the multi-line values collected while reading

all_sections =[self._defaults]

all_sections.extend(self._sections.values())for options inall_sections:for name, val inoptions.items():ifisinstance(val, list):

options[name]= '\n'.join(val)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值