473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Python ConfigParser

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
3 7177
bvdet
2,851 Expert Mod 2GB
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
That works!
So ConfigParser will only read options with unique name then?

Thanks
May 24 '07 #3
bvdet
2,851 Expert Mod 2GB
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

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

Similar topics

5
by: kael | last post by:
Hello, I'm trying to run _Newspipe_ but Python returns an error : ----------------------------------------------------------------------- # python2.3 /home/kael/newspipe/newspipe.py...
12
by: Karlo Lozovina | last post by:
I've been Googling around for _small_, flat file (no server processes), SQL-like database which can be easily access from Python. Speed and perforamnce are of no issue, most important is that all...
4
by: siggi | last post by:
Hi all, newbie question: I'd like to try speech synthesis with PythonWin 2.5. Problem ****** according to several instructions, such as found on...
0
by: Joe Goldthwaite | last post by:
Thanks Guilherme. That helped. I guess I was thinking that pysqlite would automatically come with some version of sqlite. The fact that it doesn't is what was causing me to get the strange...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.