OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How can I parse a .ini file with this type of format?

  • Thread starter Thread starter user3612
  • Start date Start date
U

user3612

Guest
I would like to parse a generated .ini file containing some config parameters, but I am a little uncertain of the format used. I am not able to change the format of the generated .ini file. This is a snippet of what the format looks like:

Code:
[Sensors]
Sensors = {
    (
        [0]={
            SensorType=Camera
            Refresh=30
        }
    )}

I am able to access the Sensors variable with the following code:

Code:
import configparser

config = configparser.ConfigParser()
path = "my_config.ini"
config.read(path)
single_sensor = config["Sensors"]["Sensors"]
print(single_sensor)

Output:

Code:
{
(
[0]={
SensorType=Camera
Refresh=30
}
)}

The problem is that this is a string that I can not parse further the same way. Let's say I want to access SensorType. Then the following syntax would not work:

Code:
single_sensor = config["Sensors"]["Sensors"]["0"]["SensorType"]

So how could I go about parsing a .ini file in this format? Would I have to parse the string separately after retrieving what I can using the .ini section syntax?
<p>I would like to parse a generated .ini file containing some config parameters, but I am a little uncertain of the format used. I am not able to change the format of the generated .ini file. This is a snippet of what the format looks like:</p>
<pre><code>[Sensors]
Sensors = {
(
[0]={
SensorType=Camera
Refresh=30
}
)}
</code></pre>
<p>I am able to access the <code>Sensors</code> variable with the following code:</p>
<pre><code>import configparser

config = configparser.ConfigParser()
path = "my_config.ini"
config.read(path)
single_sensor = config["Sensors"]["Sensors"]
print(single_sensor)
</code></pre>
<p>Output:</p>
<pre><code>{
(
[0]={
SensorType=Camera
Refresh=30
}
)}
</code></pre>
<p>The problem is that this is a string that I can not parse further the same way. Let's say I want to access <code>SensorType</code>. Then the following syntax would not work:</p>
<pre><code>single_sensor = config["Sensors"]["Sensors"]["0"]["SensorType"]
</code></pre>
<p>So how could I go about parsing a .ini file in this format? Would I have to parse the string separately after retrieving what I can using the .ini section syntax?</p>
 

Latest posts

A
Replies
0
Views
1
AgencyAnalytics
A
S
Replies
0
Views
1
Stacker Media
S
C
Replies
0
Views
1
CC.Talent
C
Top