Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use configparser in python3

Shulou Source: shulou.com Published: 2022-06-03 04:43:43 09月10日 Update

This article mainly introduces how to use configparser in python3, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.

1. Create a configparser file

Import configparser # Import module config = configparser.ConfigParser () # Note case and () config ['DEFAULT'] = {' Server': '45mm,' Compression': 'yes'} config [' server'] = {'deletehq':'0',' localtime':'20180706', 'port':'22'} config [' system'] = {'market64':'xiadan1.exe' 'market128':'xiadan2.exe',' market256':'xiadan3.exe'} config ['client'] = {} with open (' configTest.ini', 'w') as configfile: config.write (configfile)

Or create it through a dictionary

Config = configparser.ConfigParser () config.read_dict ({'section1': {' key1': 'value1',' key2': 'value2',' key3': 'value3'},' section2': {'keyA':' valueA') 'keyB': 'valueB',' keyC': 'valueC'},' section3': {'foo':' x, 'bar': 'y' 'fun:' z'}}) with open ('configTest1.ini',') as configfile: config.write (configfile)

two。 Read configuration file

Config.read ('configTest.ini') [DEFAULT] server = 45compression = YES [server] deletehq = 0localtime = 20180706port = 22 [system] market64 = xiadan1.exemarket128 = xiadan2.exemarket256 = xiadan3.exe [client]

3. Read operation

Get all sectionsprint (config.sections ()) ['server',' system', 'client'] get the keys & valuesprint (config.items (' system')) of the specified section [('server',' 45'), ('compression',' yes'), ('market64',' xiadan1.exe'), ('market128',' xiadan2.exe'), ('market256') 'xiadan3.exe')] # Note the string returned by items () will be all lowercase to get the keysprint of the specified section (config.options (' system')) ['market64',' market128', 'market256',' server', 'compression'] # will print the keys in the default to get the valueprint (config [' system'] ['market64']) xiadan1.exe of the specified key

4. Check

'section' in config'option' in config ['section'] config.has_section [' section'] config.hais_option ['section','option']

5. Add

Config.add_section ('section4') config.set (' section4','key1','value1') config.write (open ('configTest1.ini','w')) # write [section4] key1 = value1

6. Delete

Config.remove_option ('section4','key1') # Delete optionconfig.remove_section (' section4') # Delete section

7. [DEFAULT]

[DEFAULT] generally contains the default entry for the ini format configuration file, so some methods of configparser will skip this section automatically. Sections () is not available, and the deletion method is not valid for [DEFAULT], but it is possible to specify that you can delete and modify keys & values in [DEFAULT]. Another special thing is that has_section () is also invalid and can be used differently from in.

Thank you for reading this article carefully. I hope the article "how to use configparser in python3" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: Articles documents methods configuration special value interest at the same time size dictionary character lowercase more friends format module knowledge editing industry information Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Huawei NVidia Docker macOS