By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,626 Members | 1,146 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,626 IT Pros & Developers. It's quick & easy.

Python ConfigParser

P: 2
I am working with ConfigParser in python and the ini file that I am reading in has a section titled [ClientList] and under that section I have multiple options. All the options have the same name "customer ="
Eample:

[ClientList]
customer = widget1
customer = widget2
customer = widget3
customer = widget4

I am trying to get a list of all the options but have only been able to get the last one to print. Here is the script that I am working with right now and the only thing it provides me is the last option in my list. Is this even possible with ConfigParser and if so can someone help me?

Thanks


Expand|Select|Wrap|Line Numbers
  1. # Import Statment 
  2. import ConfigParser 
  3. import os 
  4.  
  5. # Script Variables 
  6. inifile = 'client.ini.txt' 
  7. client = {}
  8.  
  9.  
  10. # Reading in config file 
  11. config = ConfigParser.ConfigParser() 
  12. config.read(inifile) 
  13.  
  14.  
  15. def main():
  16.     x = 0
  17.     for section in config.sections(): 
  18.         if section == "ClientList": 
  19.             for option in config.options(section):
  20.                 client[x] = config.get(section, option)
  21.                 #print " ", option, "=", config.get(section, option)
  22.                 print client
  23.                 x = x+1
  24.  
  25. if __name__=="__main__": 
  26.  
  27.     main()
  28.  
May 23 '07 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
I am working with ConfigParser in python and the ini file that I am reading in has a section titled [ClientList] and under that section I have multiple options. All the options have the same name "customer ="
Eample:

[ClientList]
customer = widget1
customer = widget2
customer = widget3
customer = widget4

I am trying to get a list of all the options but have only been able to get the last one to print. Here is the script that I am working with right now and the only thing it provides me is the last option in my list. Is this even possible with ConfigParser and if so can someone help me?

Thanks


Expand|Select|Wrap|Line Numbers
  1. # Import Statment 
  2. import ConfigParser 
  3. import os 
  4.  
  5. # Script Variables 
  6. inifile = 'client.ini.txt' 
  7. client = {}
  8.  
  9.  
  10. # Reading in config file 
  11. config = ConfigParser.ConfigParser() 
  12. config.read(inifile) 
  13.  
  14.  
  15. def main():
  16.     x = 0
  17.     for section in config.sections(): 
  18.         if section == "ClientList": 
  19.             for option in config.options(section):
  20.                 client[x] = config.get(section, option)
  21.                 #print " ", option, "=", config.get(section, option)
  22.                 print client
  23.                 x = x+1
  24.  
  25. if __name__=="__main__": 
  26.  
  27.     main()
  28.  
'INI' files generally have only unique variable names. Therefore it's no surprise that only the last customer is returned. Do something like this to get your data:
Expand|Select|Wrap|Line Numbers
  1. f = open(fn)
  2. for line in f:
  3.     outList = []
  4.     while '[ClientList]' not in line:
  5.         line = f.next()
  6.     line = f.next()
  7.     while not line.startswith('['):
  8.         outList.append(line.strip())
  9.         line = f.next()
  10.     break
  11. f.close()
  12. print outList
>>> ['customer = widget1', 'customer = widget2', 'customer = widget3', 'customer = widget4', '']
May 24 '07 #2

P: 2
That works!
So ConfigParser will only read options with unique name then?

Thanks
May 24 '07 #3

bvdet
Expert Mod 2.5K+
P: 2,851
That works!
So ConfigParser will only read options with unique name then?

Thanks
You are welcome. This works:
Expand|Select|Wrap|Line Numbers
  1. p = ConfigParser()
  2. p.readfp(file_object)
  3. print p.items('ClientList')
>>> [('customer1', 'widget1'), ('customer3', 'widget3'), ('customer2', 'widget2'), ('customer4', 'widget4')]

Unique names are required. The instance data is maintained as a nested dictionary.
Expand|Select|Wrap|Line Numbers
  1. >>> p.__dict__['_sections']['ClientList']
  2. {'customer1': 'widget1', '__name__': 'ClientList', 'customer3': 'widget3', 'customer2': 'widget2', 'customer4': 'widget4'}
  3. >>>
May 24 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.