LoginSignup
0
0

More than 1 year has passed since last update.

python configparser

Last updated at Posted at 2022-10-28

INIファイル読み込み

IPInfo.ini
[Server1]
IP=192.168.0.123|192.168.0.456
[Server2]
IP=192.168.0.78|192.168.0.90
import configparser

# 設定ファイルのパス
CONF_FILEPATH = 'IPInfo.ini'
config = configparser.ConfigParser()
config.read(CONF_FILEPATH, 'UTF-8')

# 書き方1
config_svr1 = config['Server1']
svr1_IP = config_svr1['IP']

'''
# 書き方2
svr1_IP = config['Server1']['IP'] 
'''

print("svr1_IP: " + svr1_IP) 

参考文献

for文でセクション周回

sectionメソッドでセクションを取得できる。

for section in config.sections():
  print("セクション: " + section )
  print("IP: " + config[section]['IP'] ) 
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0