Read and Parse key=value; config file


/ Published in: Python
Save to your folder(s)

this stores the key=value pairs in a dictionary called cvars
perfect for config files
as always comments/critisim welcomed!


Copy this code and paste it in your HTML
  1. cvars = {}
  2.  
  3. def loadConfig(file):
  4.  
  5. configfile = open(file,'r')
  6. configlist = configfile.readlines()
  7. for x in configlist:
  8. if x[:1] == '[' or x[:1] == '/':
  9. pass
  10. else:
  11. vars = x.replace(';','').split('=')
  12.  
  13. cvars[vars[0]] = vars[1]

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.