In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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!
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.