1

I have an INI file

[default]
hosts=030, 031, 032

where I have comma separated values. I can read all values with a simple

comma_separated_values=config['default']['hosts']

This way I can get all the values in a variable. But how can I iterate over this INI file so that I can store all these values as a list rather variable.

Andreas
  • 5,393
  • 9
  • 44
  • 53
Rob
  • 150
  • 1
  • 4
  • 17
  • Possible duplicate of [Iterate over sections in a config file](https://stackoverflow.com/questions/22068050/iterate-over-sections-in-a-config-file) – Martin Jun 14 '17 at 04:08

4 Answers4

3

Assuming the values are required to be integers, you would want to convert them to integers after extracting a list from the comma-separated string.

Following on from Colwin's answer:

values_list = [int(str_val) for str_val in config['default']['hosts'].split(',')]

Or if the zero prefixes to each number are supposed to indicate that they are octal:

values_list = [int(str_val, 8) for str_val in config['default']['hosts'].split(',')]
David Scarlett
  • 3,171
  • 2
  • 12
  • 28
  • Thank you @David , the above answer is exactly what I was looking for...When I read the INI file it was being stored as a unicode type but your code helped me to store them as a int and also as a list. Thank you for your help. – Rob Jun 14 '17 at 13:41
2

Since these are getting read in as a string, you should be able to do this and store it in a list

values_list = config['default']['hosts'].split(',')
Colwin
  • 2,655
  • 3
  • 25
  • 25
2

You can generalize it as follows :

import ConfigParser
import io

# Load the configuration file
def read_configFile():
    config = ConfigParser.RawConfigParser(allow_no_value=True)
    config.read("config.ini")
    # List all contents
    print("List all contents")
    for section in config.sections():
        #print("Section: %s" % section)
        for options in config.options(section):
            if (options == 'port'):
                a = config.get(section,options).split(',')
                for i in range(len(a)):
                    print("%s:::%s" % (options,  a[i]))

            else:
                print("%s:::%s" % (options,  config.get(section, options)))

read_configFile()


config.ini
[mysql]
host=localhost
user=root
passwd=my secret password
db=write-math
port=1,2,3,4,5

[other]
preprocessing_queue = ["preprocessing.scale_and_center",
"preprocessing.dot_reduction",
"preprocessing.connect_lines"]

use_anonymous=yes
0

You can read the contents of the file and split it using split(','). Try it using the below code.

with open('#INI FILE') as f:
    lines = f.read().split(',')
print(lines) # Check your output
print (type(lines)) # Check the type [It will return a list]
viraptor
  • 33,322
  • 10
  • 107
  • 191
Niranjan
  • 1
  • 4
  • The poster indicated that they can access the string of comma-separated values as `config['default']['hosts']`. This indicates that they have already parsed the INI file (probably using something like the `ConfigParser` module) and it would be a major step backwards to attempt to parse the file as unformatted text via the `file.read()` function. – David Scarlett Jun 14 '17 at 05:12