擅长:python、mysql、java
<p>您可能正在寻找<a href="https://docs.python.org/3/library/configparser.html" rel="nofollow noreferrer">configparser</a>,它正是为此而设计的!在</p>
<p><strong>构建新配置</strong></p>
<pre><code>>>> import configparser
>>> config = configparser.ConfigParser()
>>> config.sections()
[]
>>> config['Players'] = {
... "ti7": 999,
... "example": 50
... }
>>> with open('example.cfg', 'w') as fh:
... config.write(fh) # write directly to file handler
...
</code></pre>
<p><strong>现在读回去</strong></p>
^{pr2}$
<p><strong>检查书面文件</strong></p>
<pre><code>% cat example.cfg
[Players]
ti7 = 999
example = 50
</code></pre>