473,320 Members | 1,839 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,320 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 7175
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.