Guest User

Untitled

a guest
Aug 13th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.02 KB | None | 0 0
  1. from ConfigParser import SafeConfigParser
  2.  
  3. config = SafeConfigParser()
  4. config.read('config.ini')
  5. config.add_section('main')
  6. config.set('main', 'key1', 'value1')
  7. config.set('main', 'key2', 'value2')
  8. config.set('main', 'key3', 'value3')
  9.  
  10. with open('config.ini', 'w') as f:
  11. config.write(f)
  12.  
  13. [main]
  14. key1 = value1
  15. key2 = value2
  16. key3 = value3
  17.  
  18. from ConfigParser import SafeConfigParser
  19.  
  20. config = SafeConfigParser()
  21. config.read('config.ini')
  22.  
  23. print config.get('main', 'key1') # -> "value1"
  24. print config.get('main', 'key2') # -> "value2"
  25. print config.get('main', 'key3') # -> "value3"
  26.  
  27. # getfloat() raises an exception if the value is not a float
  28. a_float = config.getfloat('main', 'a_float')
  29.  
  30. # getint() and getboolean() also do this for their respective types
  31. an_int = config.getint('main', 'an_int')
  32.  
  33. import json
  34.  
  35. config = {'key1': 'value1', 'key2': 'value2'}
  36.  
  37. with open('config.json', 'w') as f:
  38. json.dump(config, f)
  39.  
  40. import json
  41.  
  42. with open('config.json', 'r') as f:
  43. config = json.load(f)
  44.  
  45. #edit the data
  46. config['key3'] = 'value3'
  47.  
  48. #write it back to the file
  49. with open('config.json', 'w') as f:
  50. json.dump(config, f)
  51.  
  52. [Section]
  53. key = value
  54. key with spaces = somevalue
  55.  
  56. #!/usr/bin/env python
  57.  
  58. import ConfigParser
  59. import io
  60.  
  61. # Load the configuration file
  62. with open("config.yml") as f:
  63. sample_config = f.read()
  64. config = ConfigParser.RawConfigParser(allow_no_value=True)
  65. config.readfp(io.BytesIO(sample_config))
  66.  
  67. # List all contents
  68. print("List all contents")
  69. for section in config.sections():
  70. print("Section: %s" % section)
  71. for options in config.options(section):
  72. print("x %s:::%s:::%s" % (options,
  73. config.get(section, options),
  74. str(type(options))))
  75.  
  76. # Print some contents
  77. print("nPrint some contents")
  78. print(config.get('other', 'use_anonymous')) # Just get the value
  79. print(config.getboolean('other', 'use_anonymous')) # You know the datatype?
  80.  
  81. List all contents
  82. Section: mysql
  83. x host:::localhost:::<type 'str'>
  84. x user:::root:::<type 'str'>
  85. x passwd:::my secret password:::<type 'str'>
  86. x db:::write-math:::<type 'str'>
  87. Section: other
  88. x preprocessing_queue:::["preprocessing.scale_and_center",
  89. "preprocessing.dot_reduction",
  90. "preprocessing.connect_lines"]:::<type 'str'>
  91. x use_anonymous:::yes:::<type 'str'>
  92.  
  93. Print some contents
  94. yes
  95. True
  96.  
  97. import os
  98. configfile_name = "config.yaml"
  99.  
  100. # Check if there is already a configurtion file
  101. if not os.path.isfile(configfile_name):
  102. # Create the configuration file as it doesn't exist yet
  103. cfgfile = open(configfile_name, 'w')
  104.  
  105. # Add content to the file
  106. Config = ConfigParser.ConfigParser()
  107. Config.add_section('mysql')
  108. Config.set('mysql', 'host', 'localhost')
  109. Config.set('mysql', 'user', 'root')
  110. Config.set('mysql', 'passwd', 'my secret password')
  111. Config.set('mysql', 'db', 'write-math')
  112. Config.add_section('other')
  113. Config.set('other',
  114. 'preprocessing_queue',
  115. ['preprocessing.scale_and_center',
  116. 'preprocessing.dot_reduction',
  117. 'preprocessing.connect_lines'])
  118. Config.set('other', 'use_anonymous', True)
  119. Config.write(cfgfile)
  120. cfgfile.close()
  121.  
  122. [mysql]
  123. host = localhost
  124. user = root
  125. passwd = my secret password
  126. db = write-math
  127.  
  128. [other]
  129. preprocessing_queue = ['preprocessing.scale_and_center', 'preprocessing.dot_reduction', 'preprocessing.connect_lines']
  130. use_anonymous = True
  131.  
  132. from BeautifulSoup import BeautifulSoup
  133.  
  134. with open("config.xml") as f:
  135. content = f.read()
  136.  
  137. y = BeautifulSoup(content)
  138. print(y.mysql.host.contents[0])
  139. for tag in y.other.preprocessing_queue:
  140. print(tag)
  141.  
  142. <config>
  143. <mysql>
  144. <host>localhost</host>
  145. <user>root</user>
  146. <passwd>my secret password</passwd>
  147. <db>write-math</db>
  148. </mysql>
  149. <other>
  150. <preprocessing_queue>
  151. <li>preprocessing.scale_and_center</li>
  152. <li>preprocessing.dot_reduction</li>
  153. <li>preprocessing.connect_lines</li>
  154. </preprocessing_queue>
  155. <use_anonymous value="true" />
  156. </other>
  157. </config>
Add Comment
Please, Sign In to add comment