Files
pysimpleini/docs-static/usage.md
2023-04-01 01:19:22 +01:00

3.1 KiB

Usage

Installation

From pypi repository (prefered):

/> python -m pip install pysimpleini

From downloaded .whl file:

/> python -m pip install pysimpleini-<VERSION>-py3-none-any.whl

From master git repository:

/> python -m pip install git+https://chacha.ddns.net/gitea/chacha/pysimpleini.git@master

Import in your project

Add this line on the top of your python script:

from pysimpleini import PySimpleINI

Basic API

Sample INI File:

[testsection]
key1=test1
key2=test2
key3=test3
key3=test31

[testsection2]
key10=test10

Load the INI File:

myini:PySimpleINI = PySimpleINI("myinifile.ini")

Access simple key values:

print(myini.getkeyvalue("testsection", "key1"))
test1
print(myini.getkeyvalue("testsection", "key2"))
test2
print(myini.getkeyvalue("testsection", "key3"))
['test3','test31']
print(myini.getkeyvalue("testsection", "key3"))
['test3','test31']

Access key values with specified index:

print(myini.getkeyvalue_ex("testsection", "key3",0))
test3
print(myini.getkeyvalue_ex("testsection", "key3",1))
test31

list keys name:

print(myini.getallkeynames("testsection"))
['key1', 'key2', 'key3', 'key3']

list sections name:

print(myini.getallsectionnames())
['testsection', 'testsection2']

add a new key:

myini.setaddkeyvalue("testsection2","key11","test11")
print(myini.format())
[testsection]
key1=test1
key2=test2
key3=test3
key3=test31
[testsection2]
key10=test10
key11=test11

update a key value:

myini.setaddkeyvalue("testsection2","key11","test13")
print(myini.format())
[testsection]
key1=test1
key2=test2
key3=test3
key3=test31
[testsection2]
key10=test10
key11=test13

create a new key with same name:

myini.setaddkeyvalue("testsection2","key11","test14",True)
print(myini.format())
[testsection]
key1=test1
key2=test2
key3=test3
key3=test31
[testsection2]
key10=test10
key11=test13
key11=test14

create a new empty section

myini.addsection("testsection3")
print(myini.format())
[testsection]
key1=test1
key2=test2
key3=test3
key3=test31
[testsection2]
key10=test10
key11=test13
key11=test14
[testsection3]

save your file

myini.writefile()

save to a new file

myini.filepath = "somedir/somefile.ini"
myini.writefile()

Read the API documentation and the unit test for more informations.

limitations

There is some known limitations.

  1. File is only written when writefile() is called.

  2. Support for more than one section with the same name is incomplete using setaddkeyvalue() / getkeyvalue_ex() / getkey_ex() / delkey_ex() because the index argument is only for keys. If more precise section selection is needed, please use getsection() to get the correct section object and then use the underlying methods.