Notes on writing config files for Python Note: configparser

Recently I'm addicted to Laravel vuejs. I've also written Python, but it's a little less. I'm glad that the service I was involved in seems to move forward a little.

I feel like I'm wandering around this, so I'll take a note for myself. It is a simple sample when reading an ini file.

This is good. I wanted this. I mean, why isn't the library provided with features? https://qiita.com/suto3/items/db6f05f943cc2ea2ef59

python


#!/usr/bin/env python                                                                                                                                         
# -*- coding:utf-8 -*-                                                                                                                                        
import os
import configparser

config = configparser.SafeConfigParser()
#Use an absolute pass. (maybe)
path = os.path.dirname(os.path.abspath(__file__))
path = os.path.join(path, 'hoge.ini')

print(path)
config.read(path)

value = config.get('general', 'hoge1')
print(value)

#======

def get_config(ini):
    for section in ini.sections():
        keys, values = get_section(ini, section)
    return keys, values

def get_section(ini, section):
    keys, values = [], []
    for key in ini.options(section):
        key, value = get_by_key(ini, section, key)
        keys.append(key)
        values.append(value)
    return keys, values

def get_by_key(ini, section, key):
    return [key, ini.get(section, key)]

keys, values = get_config(config)
print(keys, values)



hoge.ini


[general]
name1=It's a name
path=/home/user/hoge/

Recommended Posts

Notes on writing config files for Python Note: configparser
Minimum grammar notes for writing Python
Notes on nfc.ContactlessFrontend () for nfcpy in python
[Introduction for beginners] Reading and writing Python CSV files
Python Tkinter notes (for myself)
[Python] Notes on data analysis
Notes on installing Python on Mac
3 months note for starting Python
Notes on installing Python on CentOS
[Python] Notes on while statements (writing style and infinite loop)
Notes on reading and writing float32 TIFF images in python
Notes on Python and dictionary types
Notes on using MeCab from Python
uproot: Python / Numpy based library for reading and writing ROOT files
Personal notes for python image processing
boto3 (AWS SDK for Python) Note
Notes for me python csv graph
Notes on installing Python using PyEnv
A note for writing Python-like code
Notes for Python file input / output
Notes on using rstrip with python.
Notes on accessing dashDB from python
Install confluent-kafka for Python on Ubuntu
python ConfigArgParse Note ~ Are you still managing arguments and config files separately? ~
WEB scraping with Python (for personal notes)
Notes on PyQ machine learning python grammar
Find files like find on linux in Python
Notes for installing Sublime Text on Ubuntu
Notes on creating static files in Django
[Note] Procedures for installing Ubuntu on Windows 10
Type annotations for Python2 in stub files!
Note for Pyjulia calling Julia from Python
Notes on doing Japanese OCR with Python
Template for writing batch scripts in python
Notes on building Python and pyenv on Mac
Reading and writing JSON files with Python
Download files on the web with Python
Notes for using python (pydev) in eclipse
Notes on using code formatter in Python
Note: Python
Character code for reading and writing csv files with python ~ windows environment ver ~
[Note] The solution for Python on MacOSX where import hashlib causes an error
[Note] Python environment construction on rental server "CORESERVER"
(Windows) Causes and workarounds for UnicodeEncodeError on Python 3
Notes on installing Python3 and using pip on Windows7
Study from Python Reading and writing Hour9 files
Notes on using dict in python [Competition Pro]
A note on optimizing blackbox functions in Python
Tweet (API 1.1) on Google App Engine for Python
Reading and writing CSV and JSON files in Python
Get note information using Evernote SDK for Python 3
Note on encoding when LANG = C in Python
Note for formatting numbers with python format function
Notes for implementing simple collaborative filtering in Python
Split files when writing vim plugin in python
THORLABS camera CS2100M-USB runs on Python [for research]
PDF files and sites useful for learning Python 3
(Note) Basic statistics on Python & Pandas on IBM DSX
Notes for using TensorFlow on Bash on Ubuntu on Windows
Reading and writing fits files with Python (memo)
Installing TensorFlow on Windows Easy for Python beginners