473,395 Members | 1,521 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,395 software developers and data experts.

.ini file parser (ConfigParser)

I've written a simple parser for .ini files; it's not a replacement for
ConfigParser, but you could build such a module ontop of it fairly
easily, or you could build something either more or less complicated.

svn://colorstudy.com/home/ianb/config/
http://colorstudy.com/cgi-bin/viewcv...e/ianb/config/

For instance, here's the code to parse an ini file into a nested
dictionary, like {'section': {'var1': ['value1']}}:
class BasicParser(INIParser):

def __init__(self):
self.data = {}

def assignment(self, name, content):
if not self.section:
self.parse_error(
'Assignments can only occur inside sections; no '
'section has been defined yet')
section = self.data.setdefault(self.section, {})
section.setdefault(name, []).append(content)

usage:

p = BasicParser()
p.feed('config.ini')
data = p.data

.... though generally the parser should be a non-public implementation
detail in a config library. Anyway, not a lot of code, but I think it's
a good piece for building different config parsers. both simple and complex.

--
Ian Bicking / ia**@colorstudy.com / http://blog.ianbicking.org
Jul 18 '05 #1
0 1033

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mike Zupan | last post by:
I need to edit the value(s) of a config file via a GUI interface. I'm trying to use ConfigParser but set() doesn't seem to write. Here are my reading and writing functions def...
7
by: Gopal | last post by:
Hello, I'm Gopal. I'm looking for a solution to the following problem: I need to create a text file config.txt having some parameters. I'm thinking of going with this format by having "Param...
1
by: s99999999s2003 | last post by:
hi i used ConfigParser to read a config file. I need the config file to have identical sections. ie : blah = "some server" blah = "some destination" end= ''
17
by: News | last post by:
Hi everyone, My goal is to pull command switches/options from a file and then assign the values to select variables which would eventually be included in a class object. The data file looks...
1
by: Lialie | last post by:
Hello,all I found it easy to read configures from a config file. But how can I set a special value to an item or write it into the original file? I have tried this: import ConfigParser config...
1
by: Tommy Grav | last post by:
Hi, I have a setup file for some numerical simulation code written in C that looks like this: dt 0.1 time 0.0 nupdate 10 noutput 100 ntotal 10000
6
by: William Manley | last post by:
According to what I read in the ConfigParser module, this should work, but it doesn't. Can anyone point out what I'm doing wrong? def writeinifile ( self, FILE='P:/INI/test.ini',...
3
by: noagbodjivictor | last post by:
Hi, I need a specific example. I have seen the docs, but I don't all the stuffs there. So basically, I need my config file to be created and read by my script. Here is a snippet # read...
3
by: jamitwidme | last post by:
Hello. I am a novice programmer and have a question I have a configuration file(configuration.cfg) I read this from reading.py using ConfigParser When I use ConfigParser.get() function, it...
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: 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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.