python ini文件读写,python操作ini

  python ini文件读写,python操作ini

  本文主要介绍了计算机编程语言中初始化设置文件的后缀名配置文件读写的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  

导入模块

 

  导入configparser # py3

  

写入

 

  config=configparserConfigParser()

  配置[默认]={

  ServerAliveInterval: 45 ,

  压缩 : 是,

  压缩级别 : 9

  }

  config[bitbucket.org]={}

  配置[位存储桶。org ][ User ]= Hg

  最高机密。服务器。com ]={ }

  绝密=配置[绝密。服务器。com ]

  顶级机密[主机端口]=50022 #改变解析器

  topsecret[ForwardX11]=no #此处相同

  配置[默认][转发X11 ]=是

  # 写入文件

  用打开( example.ini , w )作为配置文件:

  配置写入(配置文件)

  

读取

 

  config=configparserConfigParser()

  config.read(example.ini )

  print(config.defaults())

  # OrderedDict([(compression , yes)])

  print(config.sections())

  # [bitbucket.org , topsecret.server.com]

  打印(配置[bitbucket.org][用户])

  #汞

  打印(配置选项(“topsecret.server.com”))

  # [端口,压缩]

  打印(配置。物品(‘绝密。服务器。com’))

  # [(压缩,是),(端口, 50022)]

  打印(配置。get(绝密。服务器。com ,端口))

  # 50022

  

修改

 

  print(config.has_section(Name ))

  # 删除

  config.remove_section(Name )

  # 添加

  config.add_section(Name )

  config[Name][name]=Tom

  config[Name][asname]=Jimi

  # 设置

  config.remove_option(Name , asname )

  config.set(Name , Name , Jack )

  # 保存

  config.write(open(example.ini , w ))

  

附:ini文件

 

  [默认]

  serveraliveinterval=45

  压缩=是

  压缩级别=9

  forwardx11=是

  [bitbucket.org]

  用户=汞

  [topsecret.server.com]

  主机端口=50022

  forwardx11=否

  帮助(configparser)

  班

  类配置解析器(RawConfigParser)

  配置解析器实现插值。

   add_section(self,section)

  在配置中创建新节。延伸

   RawConfigParser.add_section,方法是验证节名是否为

  一个字符串。

   set(self,section,option,value=

  None)

             Set an option.  Extends RawConfigParser.set by validating type and

             interpolation syntax on the value.

         defaults(self)

         get(self, section, option, *, raw=False, vars=None, fallback=<object object at 0x0000000002F42120>)

             Get an option value for a given section.

         getboolean(self, section, option, *, raw=False, vars=None, fallback=<object object at 0x0000000002F42120>)

         getfloat(self, section, option, *, raw=False, vars=None, fallback=<object object at 0x0000000002F42120>)

         getint(self, section, option, *, raw=False, vars=None, fallback=<object object at 0x0000000002F42120>)

         has_option(self, section, option)

             Check for the existence of a given option in a given section.

             If the specified `section is None or an empty string, DEFAULT is

             assumed. If the specified `section does not exist, returns False.

         has_section(self, section)

             Indicate whether the named section is present in the configuration.

         items(self, section=<object object at 0x0000000002F42120>, raw=False, vars=None)

             Return a list of (name, value) tuples for each option in a section.

         options(self, section)

             Return a list of option names for the given section name.

         popitem(self)

             Remove a section from the parser and return it as

         read(self, filenames, encoding=None)

             Read and parse a filename or a list of filenames.

             Return list of successfully read files.

         read_dict(self, dictionary, source=<dict>)

             Read configuration from a dictionary.

         read_file(self, f, source=None)

             Like read() but the argument must be a file-like object.

         read_string(self, string, source=<string>)

             Read configuration from a given string.

         readfp(self, fp, filename=None)

             Deprecated, use read_file instead.

         remove_option(self, section, option)

             Remove an option.

         remove_section(self, section)

             Remove a file section.

         sections(self)

             Return a list of section names, excluding [DEFAULT]

         write(self, fp, space_around_delimiters=True)

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

         clear(self)

             D.clear() -> None.  Remove all items from D.

         pop(self, key, default=<object object at 0x0000000002F42040>)

             D.pop(k[,d]) -> v, remove specified key and return the corresponding value.

             If key is not found, d is returned if given, otherwise KeyError is raised.

         setdefault(self, key, default=None)

             D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D

         update(*args, **kwds)

             D.update([E, ]**F) -> None.  Update D from mapping/iterable E and F.

             If E present and has a .keys() method, does:     for k in E: D[k] = E[k]

             If E present and lacks .keys() method, does:     for (k, v) in E: D[k] = v

             In either case, this is followed by: for k, v in F.items(): D[k] = v

         keys(self)

             D.keys() -> a set-like object providing a view on Ds keys

         values(self)

             D.values() -> an object providing a view on Ds values

  """

  到此这篇关于Python中ini配置文件读写的实现的文章就介绍到这了,更多相关Python ini文件读写内容请搜索盛行IT软件开发工作室以前的文章或继续浏览下面的相关文章希望大家以后多多支持盛行IT软件开发工作室!

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: