Python使用configparser方法如何實(shí)現(xiàn)對(duì)配置文件進(jìn)行讀寫(xiě)-創(chuàng)新互聯(lián)

Python使用configparser方法如何實(shí)現(xiàn)對(duì)配置文件進(jìn)行讀寫(xiě)?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

成都創(chuàng)新互聯(lián)公司服務(wù)緊隨時(shí)代發(fā)展步伐,進(jìn)行技術(shù)革新和技術(shù)進(jìn)步,經(jīng)過(guò)十多年的發(fā)展和積累,已經(jīng)匯集了一批資深網(wǎng)站策劃師、設(shè)計(jì)師、專(zhuān)業(yè)的網(wǎng)站實(shí)施團(tuán)隊(duì)以及高素質(zhì)售后服務(wù)人員,并且完全形成了一套成熟的業(yè)務(wù)流程,能夠完全依照客戶要求對(duì)網(wǎng)站進(jìn)行成都做網(wǎng)站、網(wǎng)站制作、建設(shè)、維護(hù)、更新和改版,實(shí)現(xiàn)客戶網(wǎng)站對(duì)外宣傳展示的首要目的,并為客戶企業(yè)品牌互聯(lián)網(wǎng)化提供全面的解決方案。

Python自身提供了一個(gè)Module - configparser,來(lái)進(jìn)行對(duì)配置文件的讀寫(xiě)


Configuration file parser.
A configuration file consists of sections, lead by a “[section]” header,
and followed by “name: value” entries, with continuations and such in
the style of RFC 822.

Note The ConfigParser module has been renamed to configparser in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

在py2中,該模塊叫ConfigParser,在py3中把字母全變成了小寫(xiě)。本文以py3為例

類(lèi)

ConfigParser的屬性和方法

ConfigParser -- responsible for parsing a list of
   configuration files, and managing the parsed database.
 
 methods:
 
 __init__(defaults=None, dict_type=_default_dict, allow_no_value=False,
  delimiters=('=', ':'), comment_prefixes=('#', ';'),
  inline_comment_prefixes=None, strict=True,
  empty_lines_in_values=True, default_section='DEFAULT',
  interpolation=<unset>, converters=<unset>):
 Create the parser. When `defaults' is given, it is initialized into the
 dictionary or intrinsic defaults. The keys must be strings, the values
 must be appropriate for %()s string interpolation.
 
 When `dict_type' is given, it will be used to create the dictionary
 objects for the list of sections, for the options within a section, and
 for the default values.
 
 When `delimiters' is given, it will be used as the set of substrings
 that divide keys from values.
 
 When `comment_prefixes' is given, it will be used as the set of
 substrings that prefix comments in empty lines. Comments can be
 indented.
 
 When `inline_comment_prefixes' is given, it will be used as the set of
 substrings that prefix comments in non-empty lines.
 
 When `strict` is True, the parser won't allow for any section or option
 duplicates while reading from a single source (file, string or
 dictionary). Default is True.
 
 When `empty_lines_in_values' is False (default: True), each empty line
 marks the end of an option. Otherwise, internal empty lines of
 a multiline option are kept as part of the value.
 
 When `allow_no_value' is True (default: False), options without
 values are accepted; the value presented for these is None.
 
 When `default_section' is given, the name of the special section is
 named accordingly. By default it is called ``"DEFAULT"`` but this can
 be customized to point to any other valid section name. Its current
 value can be retrieved using the ``parser_instance.default_section``
 attribute and may be modified at runtime.
 
 When `interpolation` is given, it should be an Interpolation subclass
 instance. It will be used as the handler for option value
 pre-processing when using getters. RawConfigParser objects don't do
 any sort of interpolation, whereas ConfigParser uses an instance of
 BasicInterpolation. The library also provides a ``zc.buildbot``
 inspired ExtendedInterpolation implementation.
 
 When `converters` is given, it should be a dictionary where each key
 represents the name of a type converter and each value is a callable
 implementing the conversion from string to the desired datatype. Every
 converter gets its corresponding get*() method on the parser object and
 section proxies.
 
 sections()
 Return all the configuration section names, sans DEFAULT.
 
 has_section(section)
 Return whether the given section exists.
 
 has_option(section, option)
 Return whether the given option exists in the given section.
 
 options(section)
 Return list of configuration options for the named section.
 
 read(filenames, encoding=None)
 Read and parse the iterable of named configuration files, given by
 name. A single filename is also allowed. Non-existing files
 are ignored. Return list of successfully read files.
 
 read_file(f, filename=None)
 Read and parse one configuration file, given as a file object.
 The filename defaults to f.name; it is only used in error
 messages (if f has no `name' attribute, the string `<&#63;&#63;&#63;>' is used).
 
 read_string(string)
 Read configuration from a given string.
 
 read_dict(dictionary)
 Read configuration from a dictionary. Keys are section names,
 values are dictionaries with keys and values that should be present
 in the section. If the used dictionary type preserves order, sections
 and their keys will be added in order. Values are automatically
 converted to strings.
 
 get(section, option, raw=False, vars=None, fallback=_UNSET)
 Return a string value for the named option. All % interpolations are
 expanded in the return values, based on the defaults passed into the
 constructor and the DEFAULT section. Additional substitutions may be
 provided using the `vars' argument, which must be a dictionary whose
 contents override any pre-existing defaults. If `option' is a key in
 `vars', the value from `vars' is used.
 
 getint(section, options, raw=False, vars=None, fallback=_UNSET)
 Like get(), but convert value to an integer.
 
 getfloat(section, options, raw=False, vars=None, fallback=_UNSET)
 Like get(), but convert value to a float.
 
 getboolean(section, options, raw=False, vars=None, fallback=_UNSET)
 Like get(), but convert value to a boolean (currently case
 insensitively defined as 0, false, no, off for False, and 1, true,
 yes, on for True). Returns False or True.
 
 items(section=_UNSET, raw=False, vars=None)
 If section is given, return a list of tuples with (name, value) for
 each option in the section. Otherwise, return a list of tuples with
 (section_name, section_proxy) for each section, including DEFAULTSECT.
 
 remove_section(section)
 Remove the given file section and all its options.
 
 remove_option(section, option)
 Remove the given option from the given section.
 
 set(section, option, value)
 Set the given option.
 
 write(fp, space_around_delimiters=True)
 Write the configuration state in .ini format. If
 `space_around_delimiters' is True (the default), delimiters
 between keys and values are surrounded by spaces.

新聞標(biāo)題:Python使用configparser方法如何實(shí)現(xiàn)對(duì)配置文件進(jìn)行讀寫(xiě)-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://muchs.cn/article18/iphdp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站商城網(wǎng)站、網(wǎng)站改版定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)