Esc*_*alo 81 python parsing configuration-files
我正在使用ConfigParser来读取脚本的运行时配置.
我希望能够灵活地不提供部分名称(有足够简单的脚本;它们不需要'部分').ConfigParser将抛出ConfigParser
异常,并且不接受该文件.
如何使ConfigParser只检索ConfigParser
没有部分名称的配置文件的元组?例如:
key1=val1
key2:val2
Run Code Online (Sandbox Code Playgroud)
我宁愿不写入配置文件.
xmo*_*xmo 41
受到jterrace的这个答案的启发,我提出了这个解决方案:
ini_str = '[root]\n' + open(ini_path, 'r').read()
ini_fp = StringIO.StringIO(ini_str)
config = ConfigParser.RawConfigParser()
config.readfp(ini_fp)
Run Code Online (Sandbox Code Playgroud)
编辑未来的googlers:截至Python 3.4+ readfp
已被弃用,StringIO
不再需要了.相反,我们可以read_string
直接使用:
with open('config_file') as f:
file_content = '[dummy_section]\n' + f.read()
config_parser = ConfigParser.RawConfigParser()
config_parser.read_string(file_content)
Run Code Online (Sandbox Code Playgroud)
ʇsә*_*ɹoɈ 35
您可以使用一行额外的代码执行此操作.(如果计算read_string()
语句,则为两行.)
在python 3中,用于itertools.chain()
模拟节标题read_file()
.
from configparser import ConfigParser
parser = ConfigParser()
with open("foo.conf") as stream:
parser.read_string("[top]\n" + stream.read()) # This line does the trick.
Run Code Online (Sandbox Code Playgroud)
在python 2中,在从配置文件中读取的数据前面加上一个节标题行,将结果包装在一个StringIO
对象中,并将其传递给readfp()
.
from configparser import ConfigParser
from itertools import chain
parser = ConfigParser()
with open("foo.conf") as lines:
lines = chain(("[top]",), lines) # This line does the trick.
parser.read_file(lines)
Run Code Online (Sandbox Code Playgroud)
无论使用哪种方法,您的配置设置都可用parser.items('top')
.
您也可以io
在python 3中使用它,从readfp()
包中的新主页导入它,但请注意,read_string()
在python 3中已弃用,因此应避免使用.
如果您不介意额外的依赖项,可以考虑使用TOML解析器而不是ConfigParser.
小智 17
您可以使用ConfigObj库来执行此操作:http://www.voidspace.org.uk/python/configobj.html
更新:在此处查找最新代码.
如果您在Debian/Ubuntu下,可以使用包管理器安装此模块:
apt-get install python-configobj
Run Code Online (Sandbox Code Playgroud)
使用示例:
from configobj import ConfigObj
config = ConfigObj('myConfigFile.ini')
config.get('key1') # You will get val1
config.get('key2') # You will get val2
Run Code Online (Sandbox Code Playgroud)
在我看来,最简单的方法是使用python的CSV解析器.这是一个读/写函数,演示了这种方法以及测试驱动程序.如果不允许多行值,这应该工作.:)
import csv
import operator
def read_properties(filename):
""" Reads a given properties file with each line of the format key=value. Returns a dictionary containing the pairs.
Keyword arguments:
filename -- the name of the file to be read
"""
result={ }
with open(filename, "rb") as csvfile:
reader = csv.reader(csvfile, delimiter='=', escapechar='\\', quoting=csv.QUOTE_NONE)
for row in reader:
if len(row) != 2:
raise csv.Error("Too many fields on row with contents: "+str(row))
result[row[0]] = row[1]
return result
def write_properties(filename,dictionary):
""" Writes the provided dictionary in key-sorted order to a properties file with each line of the format key=value
Keyword arguments:
filename -- the name of the file to be written
dictionary -- a dictionary containing the key/value pairs.
"""
with open(filename, "wb") as csvfile:
writer = csv.writer(csvfile, delimiter='=', escapechar='\\', quoting=csv.QUOTE_NONE)
for key, value in sorted(dictionary.items(), key=operator.itemgetter(0)):
writer.writerow([ key, value])
def main():
data={
"Hello": "5+5=10",
"World": "Snausage",
"Awesome": "Possum"
}
filename="test.properties"
write_properties(filename,data)
newdata=read_properties(filename)
print "Read in: "
print newdata
print
contents=""
with open(filename, 'rb') as propfile:
contents=propfile.read()
print "File contents:"
print contents
print ["Failure!", "Success!"][data == newdata]
return
if __name__ == '__main__':
main()
Run Code Online (Sandbox Code Playgroud)
自己遇到这个问题后,我为ConfigParser(Python 2中的版本)编写了一个完整的包装器,它可以透明地读取和写入没有部分的文件,基于Alex Martelli的方法与已接受的答案相关联.它应该是对ConfigParser的任何使用的替代品.发布它以防有需要的人找到这个页面.
import ConfigParser
import StringIO
class SectionlessConfigParser(ConfigParser.RawConfigParser):
"""
Extends ConfigParser to allow files without sections.
This is done by wrapping read files and prepending them with a placeholder
section, which defaults to '__config__'
"""
def __init__(self, *args, **kwargs):
default_section = kwargs.pop('default_section', None)
ConfigParser.RawConfigParser.__init__(self, *args, **kwargs)
self._default_section = None
self.set_default_section(default_section or '__config__')
def get_default_section(self):
return self._default_section
def set_default_section(self, section):
self.add_section(section)
# move all values from the previous default section to the new one
try:
default_section_items = self.items(self._default_section)
self.remove_section(self._default_section)
except ConfigParser.NoSectionError:
pass
else:
for (key, value) in default_section_items:
self.set(section, key, value)
self._default_section = section
def read(self, filenames):
if isinstance(filenames, basestring):
filenames = [filenames]
read_ok = []
for filename in filenames:
try:
with open(filename) as fp:
self.readfp(fp)
except IOError:
continue
else:
read_ok.append(filename)
return read_ok
def readfp(self, fp, *args, **kwargs):
stream = StringIO()
try:
stream.name = fp.name
except AttributeError:
pass
stream.write('[' + self._default_section + ']\n')
stream.write(fp.read())
stream.seek(0, 0)
return ConfigParser.RawConfigParser.readfp(self, stream, *args,
**kwargs)
def write(self, fp):
# Write the items from the default section manually and then remove them
# from the data. They'll be re-added later.
try:
default_section_items = self.items(self._default_section)
self.remove_section(self._default_section)
for (key, value) in default_section_items:
fp.write("{0} = {1}\n".format(key, value))
fp.write("\n")
except ConfigParser.NoSectionError:
pass
ConfigParser.RawConfigParser.write(self, fp)
self.add_section(self._default_section)
for (key, value) in default_section_items:
self.set(self._default_section, key, value)
Run Code Online (Sandbox Code Playgroud)
Blueicefield的回答提到了configobj,但原始的lib只支持Python 2.它现在有一个Python 3+兼容端口:
https://github.com/DiffSK/configobj
API没有改变,请看它的文档.
归档时间: |
|
查看次数: |
77289 次 |
最近记录: |