Python Forum

Full Version: Read ini file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

When running the below code, I get an error message but I don't understand it. I appreciate some insights.
TIA

data.ini
[settings]
volume = "99"
tz = "2"
first-1 = "a"
first-2 = "b"
first-3 = "c"
readini.py
try:
    from configparser import ConfigParser
except ImportError:
    from ConfigParser import ConfigParser  # ver. < 3.0

# instantiate
config = ConfigParser()

# parse existing file
config.read('data.ini')

def readInstance(instance):
    if instance in config.sections(): #checks if the given instance actually exists in the config file
        volume = config.getint[instance]["volume"].strip('\"') #read the specific config data and then convert it into an integer
        tz = config.getint[instance]["tz"].strip('\"') # repeat process for ending
        print volume
        print tz

readInstance("settings")
#print config.getint('settings', 'volume').strip('\"')
error msg.
Traceback (most recent call last):
  File "readini.py", line 24, in <module>
    readInstance("settings")
  File "readini.py", line 14, in readInstance
    volume = config.getint[instance]["volume"].strip('\"') #read the specific config data and then convert it into an integer
TypeError: 'instancemethod' object has no attribute '__getitem__'
Hello,
I am not very familiar with the module, but after quickly glancing the examples in docs, you may not be using getint() right
example:
an_int = config.getint('Section1', 'an_int')
Thank you. modified the code but getting a different error. I guess I'm not able to strip the '"'

try:
    from configparser import ConfigParser
except ImportError:
    from ConfigParser import ConfigParser  # ver. < 3.0

# instantiate
config = ConfigParser()

# parse existing file
config.read('data.ini')

def readInstance(instance):
    if instance in config.sections(): #checks if the given instance actually exists in the config file
        #read the specific config data and then convert it into an integer        
        volume = config.getint(instance,'volume').strip('\"')
        # repeat process for ending
        tz = config.getint(instance,'tz').strip('\"') # repeat process for ending
        print volume
        print tz

readInstance("settings")
#print config.getint('settings', 'volume').strip('\"')
error:
Traceback (most recent call last):
  File "readini.py", line 27, in <module>
    readInstance("settings")
  File "readini.py", line 15, in readInstance
    volume = config.getint(instance,'volume')
  File "/home/pi/.local/lib/python2.7/site-packages/backports/configparser/__init__.py", line 834, in getint
    return self._get_conv(section, option, int, **kwargs)
  File "/home/pi/.local/lib/python2.7/site-packages/backports/configparser/__init__.py", line 822, in _get_conv
    return self._get(section, conv, option, **kwargs)
  File "/home/pi/.local/lib/python2.7/site-packages/backports/configparser/__init__.py", line 814, in _get
    return conv(self.get(section, option, **kwargs))
ValueError: invalid literal for int() with base 10: '"99"'

Solved using config.get instead of config.getint.
config.get will probably determine the data type automatically, which could sometime have undesirable effects.
If you want to strictly enforce getting an integer value, using getint would be a better choice.
The problem in this case is in the data.ini file.
volume = "99"
# should be
volume = 99
your file should look like this
Output:
[settings] volume = 99 tz = 2 first-1 = a first-2 = b first-3 = c
then
try:
    from configparser import ConfigParser
except ImportError:
    from ConfigParser import ConfigParser  # ver. < 3.0
 
# instantiate
config = ConfigParser()
 
# parse existing file
config.read('data.ini')
 
def readInstance(instance):
    if instance in config.sections(): #checks if the given instance actually exists in the config file
        #read the specific config data and then convert it into an integer        
        volume = config.getint(instance,'volume')
        # repeat process for ending
        tz = config.getint(instance,'tz') # repeat process for ending
    return (volume, tz)

vol, tz = readInstance("settings")
print('volume: --> type: {} --> value: {}'.format(type(vol), vol))
print('tz: --> type: {} --> value: {}'.format(type(tz), tz))
Output:
volume: --> type: <type 'int'> --> value: 99 tz: --> type: <type 'int'> --> value: 2 >>>
I personally would do it a bit differently - i.e. with try/except and printing message if section/option is missing, or using get and supply default value like None
Thank you but unfortunately, I don’t have control over the ini file which is generated by a web form through a php code. Nor I have a monitor to see the output since I’m running the code on a headless Raspberry PI.