Solved

Read everything from a Windows .ini file

Posted on 2004-04-02
4
8,343 Views
Last Modified: 2007-12-19
Hello!
Is is possible to read all entries from a Windows .ini file in Python, also if you don't know exactly the names of sections and values? (see sample .ini file below). I would like to have some generic code that can read all sections and values, whatever they are.

[Section1]
Entry1=test
Entry2=test2

[Section2]
debug=1
Name=test
0
Comment
Question by:rj2
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Glowingdark
Comment Utility
Its a bit ugly, but it works.

# parse ini file
myfile = open('c:\sample.ini', 'r')
sections = []
keysandvalues = []

lines = []
lines = myfile.readlines()
myfile.close()
section = 'no section'

for line in lines:
    if line[0:1] == "[":
        if len(keysandvalues) > 0:
            # new section
            sections.append([section, keysandvalues])
            section = line.strip()
            keysandvalues = []
        elif section <> 'no section':
            #empty section
            sections.append([section, []])
            section = line.strip()
        else:
            # first section
            section = line.strip()
    elif line.find('=') > 1:
        #key value pair
        key, value = line.split('=')
        keysandvalues.append([key.strip(),value.strip()])
    else:
        pass

# last section
sections.append([section, keysandvalues])
   
the [sections]  will still have their brackets around them, and I dont like manually appending the last section, but it works

Kevin
0
 
LVL 24

Expert Comment

by:fridom
Comment Utility
Use the module ConfigParser
http://www.python.org/doc/current/lib/module-ConfigParser.html

Regards
Friedrich
0
 
LVL 10

Author Comment

by:rj2
Comment Utility
fridom,
Do you got a sample that demonstrates this?
(read all values from ini file and prints out their values using the module ConfigParser?)
0
 
LVL 24

Accepted Solution

by:
fridom earned 250 total points
Comment Utility
Have you read the API description? Here's your .cfg file:
[Section1]
Entry1=test
Entry2=test2

[Section2]
debug=1
Name=test

here's a code to print it.
import ConfigParser

cp = ConfigParser.ConfigParser()
cp.readfp (open('config.cfg'))

for section in cp.sections():
    print (section)
    for item in cp.items(section):
        print ("key = " + item[0] + " value = " + item[1])


Ausgabe:
Section2
key = debug value = 1
key = name value = test
Section1
key = entry2 value = test2
key = entry1 value = test

Friedrich
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now