Day⑥:configparser Module

Source: Internet
Author: User

used to generate and modify common configuration documents, the name of the current module is changed to Configparser in Python version 3.x
I. Generating a configpaser document
  
 
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. __author__ = ‘yaobin‘
  4. #生成一个configparse文档
  5. import configparser
  6. config = configparser.ConfigParser()
  7. #生成第一个节段
  8. config["DEFAULT"] = {‘ServerAliveInterval‘: ‘45‘,
  9. ‘Compression‘: ‘yes‘,
  10. ‘CompressionLevel‘: ‘9‘}
  11. #生成第二个节段
  12. config[‘bitbucket.org‘] = {}
  13. config[‘bitbucket.org‘][‘User‘] = ‘hg‘
  14. config[‘bitbucket.org‘][‘passwd‘] = ‘123456‘
  15. #生成第三个节段
  16. config[‘topsecret.server.com‘] = {}
  17. topsecret = config[‘topsecret.server.com‘]
  18. topsecret[‘Host Port‘] = ‘50022‘ # mutates the parser
  19. topsecret[‘ForwardX11‘] = ‘no‘ # same here
  20. config[‘DEFAULT‘][‘ForwardX11‘] = ‘yes‘
  21. with open(‘example.ini‘, ‘w‘) as configfile:
  22. config.write(configfile)




Two. Read and delete changes
  
 
  1. #!/usr/bin/env python
  2. #coding=utf-8
  3. __author__ = ‘yaobin‘
  4. import configparser
  5. config = configparser.ConfigParser()
  6. config.read(‘example.ini‘)
  7. # ########## 读 ##########
  8. # secs = config.sections()
  9. # print(secs)
  10. # options = config.options(‘bitbucket.org‘)
  11. # print(options)
  12. # item_list = config.items(‘bitbucket.org‘)
  13. # print(item_list)
  14. # val = config.get(‘bitbucket.org‘,‘user‘)
  15. # val2 = config.getint(‘bitbucket.org‘,‘passwd‘)
  16. # print(val)
  17. # print(val2)
  18. # a="bitbucket.org" in config
  19. # print(a)
  20. #
  21. # b="bitbucket.orgno" in config
  22. # print(b)
  23. # d=config[‘bitbucket.org‘][‘User‘]
  24. # e=config[‘DEFAULT‘][‘Compression‘]
  25. # print(d)
  26. # print(e)
  27. # topsercret=config[‘topsecret.server.com‘]
  28. # print(topsercret[‘forwardx11‘])
  29. # print(topsercret[‘host port‘])
  30. # for key in config[‘bitbucket.org‘]:
  31. # print(key) #DEFAULT也打印出来了,DEFAULT是全局变量吧
  32. #print(config[‘bitbucket.org‘][‘ForwardX11‘]) #打印默认的出来了
  33. # ########## 改写 ##########
  34. # sec = config.remove_section(‘bitbucket.org‘)
  35. # config.write(open(‘a.cfg‘, "w"))
  36. # sec = config.has_section(‘bitbucket.org‘)
  37. # print(sec)
  38. # config.add_section(‘yaobin‘)
  39. # config.write(open(‘b.cfg‘, "w"))
  40. #
  41. # config.set(‘yaobin‘,‘k1‘,"11111")
  42. # config.write(open(‘c.cfg‘, "w"))
  43. #
  44. # config.remove_option(‘bitbucket.org‘,‘passwd‘)
  45. # config.write(open(‘d.cfg‘, "w"))



From for notes (Wiz)

Day⑥:configparser Module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.