是否可以从字符串或列表中读取 ConfigParser
的配置?
文件系统上没有任何类型的临时文件
或
有没有类似的解决方案?
最佳答案
您可以使用行为类似于文件的缓冲区: Python 3 解决方案
import configparser
import io
s_config = """
[example]
is_real: False
"""
buf = io.StringIO(s_config)
config = configparser.ConfigParser()
config.read_file(buf)
print(config.getboolean('example', 'is_real'))
在 Python 2.7 中,这个实现是正确的:
import ConfigParser
import StringIO
s_config = """
[example]
is_real: False
"""
buf = StringIO.StringIO(s_config)
config = ConfigParser.ConfigParser()
config.readfp(buf)
print config.getboolean('example', 'is_real')
关于python - 如何从字符串或列表中读取配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21766451/