473,748 Members | 2,891 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Python ConfigParser

2 New Member
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 7188
bvdet
2,851 Recognized Expert Moderator Specialist
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
derf912
2 New Member
That works!
So ConfigParser will only read options with unique name then?

Thanks
May 24 '07 #3
bvdet
2,851 Recognized Expert Moderator Specialist
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
1313
by: kael | last post by:
Hello, I'm trying to run _Newspipe_ but Python returns an error : ----------------------------------------------------------------------- # python2.3 /home/kael/newspipe/newspipe.py newspipe.py - version 1.1.1 revision 1.42, Copyright (C) 2003-2004 Ricardo M. Reyes <reyesric@ufasta.edu.ar> Traceback (most recent call last):
12
3750
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 data is contained within single file and no server binary has to run in order to use the dbase. Oh, and I'm using Python under Cygwin. Ofcourse, ease of use and simplicity is most welcomed :). I'm currently playing around SQLite+PySQLite and...
4
4376
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 http://surguy.net/articles/speechrecognition.xml and in a book on Python,
0
2572
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 results. I downloaded the Windows version of the SQLite3.dll. I didn't know where to put it so I first put it in its own directory and tried to register it. That didn't work so I just moved it to the pysqlite directory in site-packages. That did...
0
8987
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9534
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9316
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4597
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4867
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.