擅长:python、mysql、java
<p>更不用说,Python中有config模块,这里是我如何处理config的,以整数值“bar”为例。+1对于alex为“”,对于False的说法,请参见值“xyz”!在</p>
<pre><code>config = {
"foo": "abcdef",
"bar": "42",
"xyz": "True" ## or 'Yes' or anything not False, "" for False
}
bar = ''
while not bar:
barinput = raw_input('Enter property bar, integer (1..99): ')
try:
if 0 < int(barinput) < 100:
pass
else:
raise ValueError("%s is not integer in range 1..99" % barinput)
except ValueError as e:
print(str(e)+"\nWrong input, try again")
else:
print("Saving correct value")
bar = config["bar"] = barinput
print('New value of "bar" in config: %i' % int(config["bar"]))
</code></pre>
<p>该值也可以在config中保存为int,但是我们不需要<code>type</code>,因为我们知道我们正在输入integer。在</p>