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 -
# Import Statment
-
import ConfigParser
-
import os
-
-
# Script Variables
-
inifile = 'client.ini.txt'
-
client = {}
-
-
-
# Reading in config file
-
config = ConfigParser.ConfigParser()
-
config.read(inifile)
-
-
-
def main():
-
x = 0
-
for section in config.sections():
-
if section == "ClientList":
-
for option in config.options(section):
-
client[x] = config.get(section, option)
-
#print " ", option, "=", config.get(section, option)
-
print client
-
x = x+1
-
-
if __name__=="__main__":
-
-
main()
-
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 -
# Import Statment
-
import ConfigParser
-
import os
-
-
# Script Variables
-
inifile = 'client.ini.txt'
-
client = {}
-
-
-
# Reading in config file
-
config = ConfigParser.ConfigParser()
-
config.read(inifile)
-
-
-
def main():
-
x = 0
-
for section in config.sections():
-
if section == "ClientList":
-
for option in config.options(section):
-
client[x] = config.get(section, option)
-
#print " ", option, "=", config.get(section, option)
-
print client
-
x = x+1
-
-
if __name__=="__main__":
-
-
main()
-
'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: - f = open(fn)
-
for line in f:
-
outList = []
-
while '[ClientList]' not in line:
-
line = f.next()
-
line = f.next()
-
while not line.startswith('['):
-
outList.append(line.strip())
-
line = f.next()
-
break
-
f.close()
-
print outList
>>> ['customer = widget1', 'customer = widget2', 'customer = widget3', 'customer = widget4', '']
That works!
So ConfigParser will only read options with unique name then?
Thanks
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: - p = ConfigParser()
-
p.readfp(file_object)
-
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. - >>> p.__dict__['_sections']['ClientList']
-
{'customer1': 'widget1', '__name__': 'ClientList', 'customer3': 'widget3', 'customer2': 'widget2', 'customer4': 'widget4'}
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics |
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):
|
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...
|
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,
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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,...
|
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...
|
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();...
| |
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |