Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python's ini configuration file

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Today, the editor will share with you the relevant knowledge points about how to use Python's ini configuration file. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

INI introduction

INI is the abbreviation of "initialization". It is used to initialize or set parameters to the operating system or specific programs. By section (section),

Key (key), value (value) composition. There are many INI files in the windows system, such as "System32.ini" and "Win.ini", which I believe are no stranger to you. Python

The module that manipulates the configuration file in is configparser, which can be used to parse files with a structure similar to the INI file on Windows.

About configparser

The module, which is called Configparser in python2 and changed to configparser until python3, is the parser used to parse the ini configuration file.

Its function is to use the three methods of RawConfigParser (), ConfigParser () and SafeConfigParser () in the module (either) to create a

Objects use object methods to add, delete, modify and check the specified configuration file.

The version of python used this time is 3.8, and the compiler uses pycharm.

INI file format

The INI configuration file consists of:

Section: represents a block consisting of square brackets and names in square brackets. The range of section is the contents of the preceding brackets to the next square brackets, such as "DEFAULT", "select", "connect_mysql".

Case and space checking: names in section are saved and obtained as is, that is, different case or spaces are different section; repeatability checks: duplicate section names are not allowed in the same configuration file.

Option: represents a configuration item in section, a key-value pair consisting of key, delimiter, and value, such as "broswer = Chrome" under "select".

Case check: key is case-insensitive. Key will be saved in lowercase automatically when it is saved into a file, but value is case-sensitive. Blank check: when you get value through key, the spaces before and after key and value in the file are automatically removed and matched, that is, when the file is saved as' broswer = Chrome', you can also get the corresponding value with 'broswer'' Chrome'; check across multiple lines: key cannot cross lines, but value can cross lines, as long as the indentation of the second and subsequent lines is different from the first line, until the next option Repetition check: like section, key under the same section is not allowed to repeat; delimiter: can be equal sign "=" or colon ":".

Comments: line comments are indicated by the pound sign "#" or the semicolon ";". In particular, it must be at the beginning of the line (there can be a space in front of it). What is used in the middle of the line will not be counted as a comment.

DEFAULT: this is a special section that can be used as an alternate value when the option of other section cannot get a value, or it can be understood that it is a root, and other section is its child section, but it is not required.

Read the instantiation of the configuration file import osimport configparserconf = configparser.ConfigParser () # class curpath = os.path.dirname (os.path.realpath (_ file__)) path = os.path.join (curpath,'read.ini') conf.read (path,encoding= "utf-8") value = conf ['select'] [' url'] print ("the value obtained through the read method is:", value) value = conf.get ('login' 'username') print (' value obtained through the get method:', value) value = conf.items ('login') # reads all the data in a section Returns a list of print ("values obtained through the items method:", value) value = conf.getint ('connect_mysql','port') # specifies the type of data to read print ("values taken out by the specified data type:", value) section = conf.sections () # reads all sectionprint (section) in the configuration file

Running result

Note: configuration file comments are in Chinese, add the parameter encoding= "utf-8" in python3, otherwise an error will be reported.

Conf.read (path,encoding= "utf-8") writes configuration file import osimport configparserconf = configparser.ConfigParser () # instantiation of class curpath = os.path.dirname (os.path.realpath (_ _ file__)) path = os.path.join (curpath,'read.ini') conf.add_section ('login') # add a new sectionconf.set (' login','username','admin') conf.set ('login','password') '123123') # write data to the configuration file conf.write (open (path,'a')) # Save the data

Looking at the configuration file after running, you can see that the new section has been written to the file.

There are two common ways to write write. The first is to delete the contents of the original file and rewrite: W

Conf.write (open (path,'w'))

The second is to continue writing content on the basis of the original file, and append mode to write: a

Conf.write (open (path,'a')) above is all the content of the article "how to use Python's ini configuration file". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report