Created
September 7, 2017 01:40
-
-
Save suganoo/0bee7ef7c07ebb891c786416a3f941ca to your computer and use it in GitHub Desktop.
ConfigParserでkeyの存在確認する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[main] | |
aaaa = hogehoge | |
bbbb = fugafuga | |
cccc = mogumogu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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 !" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[('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