In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how to read and write ini configuration files in Python. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Import module import configparser # py3 write config = configparser.ConfigParser () config ["DEFAULT"] = {'ServerAliveInterval':' 45th, 'Compression':' yes' 'CompressionLevel':' 9'} config ['bitbucket.org'] = {} config [' bitbucket.org'] ['User'] =' hg'config ['topsecret.server.com'] = {} topsecret = config [' topsecret.server.com'] topsecret ['Host Port'] =' 50022'# mutates the parsertopsecret ['ForwardX11'] =' no' # same hereconfig ['DEFAULT'] [' ForwardX11'] = 'yes'# writes to the file with open (' example.ini') 'w') as configfile: config.write (configfile) read config = configparser.ConfigParser () config.read ("example.ini") print (config.defaults ()) # OrderedDict ([(' compression', 'yes')]) print (config.sections ()) # [' bitbucket.org', 'topsecret.server.com'] print (config [' bitbucket.org'] ['User']) # hgprint (config.options ("topsecret.server.com")) # [' port' 'compression'] print (config.items ("topsecret.server.com")) # [(' compression', 'yes'), (' port', '50022')] print (config.get ("topsecret.server.com") "port") # 50022 modify print (config.has_section ("Name")) # delete config.remove_section ("Name") # add config.add_section ("Name") config ["Name"] ["name"] = "Tom" config ["Name"] ["asname"] = "Jimi" # set config.remove_option ("Name", "asname") config.set ("Name", "name") "Jack") # Save config.write (open ("example.ini", "w")) attached: ini file [DEFAULT] serveraliveinterval = 45compression = 9forwardx11 = yes [bitbucket.org] user = HG [topsecret.server.com] host port = 50022forwardx11 = no
Help (configparser)
"" CLASSES class ConfigParser (RawConfigParser) | ConfigParser implementing interpolation. | | add_section (self, section) | Create a new section in the configuration. Extends | RawConfigParser.add_section by validating if the section name is | a string. | | 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=) | Get an option value for a given section. | | getboolean (self, section, option, *, raw=False, vars=None, fallback=) | | getfloat (self, section, option, *, raw=False, vars=None, fallback=) | getint (self, section, option, *, raw=False, vars=None, fallback=) | 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=, 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='') | 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='') | 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=) | 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 (kscod), 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 D's keys | | values (self) | D.values ()-> an object providing a view on D's values | "about how to read and write ini configuration files in Python" is here. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.