Solved

Read everything from a Windows .ini file

Posted on 2004-04-02
4
8,347 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
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 250 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Python negative lookahead for aaa:bbb@hostname 6 108
Can't install scipy on Python 2.7 in Win7 2 217
Windows Command Line in Python 16 68
regex to extract ip:john 17 72
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
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…

813 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

18 Experts available now in Live!

Get 1:1 Help Now