ホーム » Python » Python iniファイル読み込み

Python iniファイル読み込み

Pythonではモジュールconfigparserを使用してiniファイルを読み込む
iniファイルの文字コードはShift-JIS((CP932)

以下のコードで基本的には読み込める
但し値に日本語があるとエラーが発生するので

[code]
import configparser

config = configparser.ConfigParser()
config.read(INIファイルのパス)
sections = config[section]
val = sections.get(key)
[/code]

文字コードを指定するとエラーを回避

[code]
import configparser

config = configparser.ConfigParser()
config.read(INIファイルのパス, ‘cp932’)
sections = config[section]
val = sections.get(key)
[/code]