gpt4 book ai didi

python - 如何从字符串或列表中读取配置?

转载 作者:IT老高 更新时间:2023-10-28 20:44:14 26 4
gpt4 key购买 nike

是否可以从字符串或列表中读取 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com