如何从字符串或列表中读取配置? [英] How to read config from string or list?
本文介绍了如何从字符串或列表中读取配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以从字符串或列表中读取 ConfigParser
的配置?
文件系统上没有任何临时文件
Is it possible to read the configuration for ConfigParser
from a string or list?
Without any kind of temporary file on a filesystem
或
有没有类似的解决方案?
OR
Is there any similar solution for this?
推荐答案
您可以使用行为类似于文件的缓冲区:Python 3 解决方案
You could use a buffer which behaves like a file: Python 3 solution
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 中,此实现是正确的:
In Python 2.7, this implementation was correct:
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')
这篇关于如何从字符串或列表中读取配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文