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 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 -
# 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
Expert Mod 2GB
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
|
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...
| |