Skip to content

Instantly share code, notes, and snippets.

@suganoo suganoo/config.ini
Created Sep 7, 2017

Embed
What would you like to do?
ConfigParserでkeyの存在確認する
[main]
aaaa = hogehoge
bbbb = fugafuga
cccc = mogumogu
# -*- coding:utf-8 -*-
import ConfigParser
inifile = ConfigParser.SafeConfigParser()
inifile.read("config.ini")
# 中身確認
print inifile.items("main")
print map(lambda x:x[0], inifile.items("main"))
# mapを使う
if "aaaa" in map(lambda x:x[0], inifile.items("main")):
print "aaaa is defined !"
else:
print "aaaa is NOT defined !"
if "dddd" in map(lambda x:x[0], inifile.items("main")):
print "dddd is defined !"
else:
print "dddd is NOT defined !"
# filterを使う コードが長くなるなぁ。。。
if len(filter(lambda x:x[0] == "aaaa", inifile.items("main"))) is not 0:
print "aaaa is defined !"
[('aaaa', 'hogehoge'), ('bbbb', 'fugafuga'), ('cccc', 'mogumogu')]
['aaaa', 'bbbb', 'cccc']
aaaa is defined !
dddd is NOT defined !
aaaa is defined !
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.