9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PythonのConfigParserで配列を読み込む方法

Posted at

はじめに

調べてみて、へぇ~と思ったのと、日本語の記事が見当たらなかったので。

ConfigParserで配列を読み込む方法

1. config.iniへの記述

ベタに配列で書いてしまう。

[Foo]
fibs: [1,1,2,3,5,8,13]

以下も可。

[Foo]
fibs = [1,1,2,3,5,8,13]

2. pythonプログラムでの読み込み

ConfigParserで読んでそのままjson.loadsで処理すると、配列になってくれる。

import configparser
import json

config_ini = configparser.ConfigParser()
config_ini.read("config.ini", encoding='utf-8')

arr = json.loads(config.get("Foo","fibs"))
print(arr)  # [1, 1, 2, 3, 5, 8, 13]

その他

数値だけじゃなく、文字列でも出来ました。

参考

9
8
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
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?