Skip to content

Instantly share code, notes, and snippets.

@suganoo
Created September 7, 2017 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save suganoo/0bee7ef7c07ebb891c786416a3f941ca to your computer and use it in GitHub Desktop.
Save suganoo/0bee7ef7c07ebb891c786416a3f941ca to your computer and use it in GitHub Desktop.
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