Rob Rob -3 years ago 160
Ini Question

Read a comma separated ini file in python?

I have a ini file

hosts=030, 031, 032

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


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.

Answer Source

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(',')]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download