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