?
Solved

Read everything from a Windows .ini file

Posted on 2004-04-02
4
Medium Priority
?
8,353 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 2

Expert Comment

by:Glowingdark
ID: 10742683
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
ID: 10746725
Use the module ConfigParser
http://www.python.org/doc/current/lib/module-ConfigParser.html

Regards
Friedrich
0
 
LVL 10

Author Comment

by:rj2
ID: 10813306
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 1000 total points
ID: 10820385
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Suggested Courses

762 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