擅长:python、mysql、java
<p>尝试使用<a href="https://docs.python.org/3/library/configparser.html#configparser.ConfigParser.read_string" rel="nofollow noreferrer">^{<cd1>}</a>方法分析配置字符串而不是文件名。然后可以使用字符串头更改内存中配置文件的格式:</p>
<pre><code>import configparser
# Slurp in file contents
with open('example.cfg') as ec:
cfg_data = ec.read()
# Insert a section header
cfg_data = "[default]\n\n" + cfg_data
# Now read the configuration:
cfgparser = configparser.ConfigParser()
cfgparser.read_string(cfg_data, source='example.cfg')
</code></pre>