Skip to main content

A Python INI File Parser and Writer

The INI File Parser

This INI file parser is simple and easy to use and yet it retains most of the stable and varying features of this WikiPedia article. The ini file syntax is very liberal

  • allows semicolon(;) and hash(#) for comment characters
  • allows equals(=), colon(:) and space for key/value delimiters
  • allows comma(,) and space as value delimiters in multi-value mode
  • allows number, boolean and string values
  • allows unquoted, single-quoted and double-quoted strings
  • allows escaped characters in strings for:
    • special characters
    • control characters
    • hexadecimal values
    • 16-bit Unicode characters
    • 32-bit Unicode characters

It supports both a single-value and multi-value mode, allowing keys to have only one or a list of multiple values. Its chief ommission is that it does not directly support subsections. However, since the period is a valid section name character, section names such as [A], [A.B] and [A.B.C] are unique section names and could be used to simulate subsections.

Implemented with a single class and five member "getter" functions, it is easy to use with a short learning curve. For example, this simple progrm would print all values for all sections and keys in the file INI_FILE_NAME.

from python_ini.ini_file import IniFile
ini = IniFile()                                 # default is single-value mode
ini.parse(INI_FILE_NAME)
if(ini.errors):
    print('INI FILE ERRORS')
    print(ini.display_errors())                 # display any errors found in the INI file
keys = ini.get_keys()                           # get a list of all keys in the global section
for key in keys:
    values = ini.get_values(key)                # get the value for each key
    print(key, end=': ')
    print(values)
sections = ini.get_sections()                   # get a list of all section names
for section in sections:
    keys = ini.get_section_keys(section)        # get a list of all keys in each section
        for key in keys:
            values = ini.get_section_values(section, key)   # get the value for each section key
            print('[' + section + ']:' + key, end=' ')
            print(values)

The INI File Writer

The writer is implemented with a simple three functions to write:

  • comment lines
  • key/value lines
  • section name lines

For overriding the default values there are three functions for setting some options:

  • delimiters
    • comment delimiters semicolon(;) or hash(#)
    • key/value delimiters equals(=), colon(:) or space
    • value list delimiters comma(,) or space
  • booleans
    • True: true, yes, on (all case insensitive)
    • False: false, no, off (all case insensitive)
    • None: none, null, void (all case insensitive)
  • the comment tab - key and section lines may have an optional comment which is tabbed on the same line. If the line is longer than the tab column the comment is tabbed on the following line.

The following simple program:

from python_ini.ini_writer import IniWriter
w = IniWriter()

# set all configurable values
w.delimiters('#', ':', ',')
w.booleans('TRUE', 'OFF', 'void')
w.comment_tab(30)

# global keys
w.comment()
w.comment('global keys')
w.key('Unicode', 'a\U0010ffffb', 'max Unicode character')
w.key('flags', [True, False, None], 'all "booleans"')

# section keys
w.comment()
w.comment('first section')
w.section('__SECTION__', 'this is a section')
w.key('section_key', [1, 2, 3])
w.key(
    'long-value',
    ['abc\xffdef\ue000ghi\U0010ffffjkl'],
    'hex and Unicode string characters')

# print the formatted INI file
w.write('output.ini')

would write the following INI file to 'output.ini'.

# global keys
Unicode: 'a\U0010ffffb'       # max Unicode character
flags: TRUE, OFF, void        # all "booleans"

# first section
[__SECTION__]                 # this is a section
section_key: 1, 2, 3
long-value: 'abc\xffdef\ue000ghi\U0010ffffjkl'
                              # hex and Unicode string characters

Installation

The Python INI file parser and writer can be installed from either Github or PyPI

Quick start guides:

Documentation

The full documentation is in the code and in additional documentation files. It can be generated with doxygen from the GitHub installation. For example, using the GitHub zip download and the Ubuntu Linux command line:

unzip python-ini-main.zip
cd python-ini-main
sudo apt install graphviz
sudo apt install doxygen
doxygen

The documentation home page will now be found in html/index.html. Or you can view it directly from the APG website

Metadata

Release files for python-ini 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-ini 1.1.0
File Size Uploaded
python-ini-1.1.0.tar.gz 23.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for python-ini 1.1.0
File Interpreter ABI Platform
python_ini-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 42.7 kB

Release files / python-ini-1.1.0.tar.gz

Download URL python-ini-1.1.0.tar.gz
Size 23.2 kB
Tags Source
SHA-256 checksum
How to use checksums
9a16089c158a69a5bd1c24cdffe82edfef33c5f992891d5e26aaba95bb370e2b
BLAKE2b-256 checksum
How to use checksums
987d46ed19ffa430bd7f54352139b04db134f37375f2154a365009517cbcf7eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release files / python_ini-1.1.0-py3-none-any.whl

Download URL python_ini-1.1.0-py3-none-any.whl
Size 19.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
31bb57c2f09dab5346d9706c3d8940a9be05dc16ea1754053be4999bad4710ae
BLAKE2b-256 checksum
How to use checksums
0e7fef95db62f691f69daad8988bd591415fd038b53529ed8cfa03b2c52b9aa7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.6

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page