How to read and write INI file with Python3?
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island
--
Chapters
00:00 Question
00:47 Accepted answer (Score 231)
01:11 Answer 2 (Score 117)
02:11 Answer 3 (Score 12)
02:26 Answer 4 (Score 10)
03:14 Thank you
--
Full question
https://stackoverflow.com/questions/8884...
Accepted answer links:
[official configparser documentation]: http://docs.python.org/py3k/library/conf...
Answer 3 links:
http://docs.python.org/library/configpar...
Answer 4 links:
[AttrDict]: https://stackoverflow.com/a/14620633/825...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #ini
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island
--
Chapters
00:00 Question
00:47 Accepted answer (Score 231)
01:11 Answer 2 (Score 117)
02:11 Answer 3 (Score 12)
02:26 Answer 4 (Score 10)
03:14 Thank you
--
Full question
https://stackoverflow.com/questions/8884...
Accepted answer links:
[official configparser documentation]: http://docs.python.org/py3k/library/conf...
Answer 3 links:
http://docs.python.org/library/configpar...
Answer 4 links:
[AttrDict]: https://stackoverflow.com/a/14620633/825...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python3x #ini
#avk47
ACCEPTED ANSWER
Score 261
This can be something to start with:
import configparser
config = configparser.ConfigParser()
config.read('FILE.INI')
print(config['DEFAULT']['path']) # -> "/path/name/"
config['DEFAULT']['path'] = '/var/shared/' # update
config['DEFAULT']['default_message'] = 'Hey! help me!!' # create
with open('FILE.INI', 'w') as configfile: # save
config.write(configfile)
You can find more at the official configparser documentation.
ANSWER 2
Score 128
Here's a complete read, update and write example.
Input file, test.ini
[section_a]
string_val = hello
bool_val = false
int_val = 11
pi_val = 3.14
Working code.
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser # ver. < 3.0
# instantiate
config = ConfigParser()
# parse existing file
config.read('test.ini')
# read values from a section
string_val = config.get('section_a', 'string_val')
bool_val = config.getboolean('section_a', 'bool_val')
int_val = config.getint('section_a', 'int_val')
float_val = config.getfloat('section_a', 'pi_val')
# update existing value
config.set('section_a', 'string_val', 'world')
# add a new section and some values
config.add_section('section_b')
config.set('section_b', 'meal_val', 'spam')
config.set('section_b', 'not_found_val', '404')
# save to a file
with open('test_update.ini', 'w') as configfile:
config.write(configfile)
Output file, test_update.ini
[section_a]
string_val = world
bool_val = false
int_val = 11
pi_val = 3.14
[section_b]
meal_val = spam
not_found_val = 404
The original input file remains untouched.
ANSWER 3
Score 15
http://docs.python.org/library/configparser.html
Python's standard library might be helpful in this case.
ANSWER 4
Score 11
The standard ConfigParser normally requires access via config['section_name']['key'], which is no fun. A little modification can deliver attribute access:
class AttrDict(dict):
def __init__(self, *args, **kwargs):
super(AttrDict, self).__init__(*args, **kwargs)
self.__dict__ = self
AttrDict is a class derived from dict which allows access via both dictionary keys and attribute access: that means a.x is a['x']
We can use this class in ConfigParser:
config = configparser.ConfigParser(dict_type=AttrDict)
config.read('application.ini')
and now we get application.ini with:
[general]
key = value
as
>>> config._sections.general.key
'value'