Reputation: 39
I am parsing an .ini file with python
import ini
def par():
with open('file.ini', 'r') as f:
contents = ini.parse(f.read())
return contents
And when executing the code gives an error
File "file.py", line 40, in par
contents = ini.parse(f.read())
File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\ini\__init__.py", line 29, in parse
data[section][key] = value
KeyError: ''
It probably gives an error because of non standard stucture for an .ini file
example of the file stucture:
[title]
a::b = 1
f::g = 12
Is there a way to parse and convert dicts to such non standart .ini files?
Upvotes: 1
Views: 114
Reputation: 3504
Use configparser
and specify the key/value delimiters.
from configparser import ConfigParser
config = ConfigParser(delimiters=("=",))
config.read("file.ini")
print(config["title"]["a::b"])
print(config["title"]["f::g"])
1 12
ConfigParser
uses =
and :
as delimiters by default. The :
causes it to parse a::b = 1
as "a": ":b = 1"
. This is why you need to restrict the delimiters to =
only.
Upvotes: 3