Python配置文件生成器 [英] Python configuration file generator

查看:115
本文介绍了Python配置文件生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python制作配置文件生成器。我的大致想法是使用模板文件和一些具有实际设置的XML文件来输入输入。然后使用该程序生成实际的配置文件。

I want to use Python to make a configuration file generator. My roughly idea is feeding input with template files and some XML files with the real settings. Then use the program to generate the real configuration files.

示例:

[template file]
server_IP = %serverip%
server_name = %servername%


[XML file]
<serverinfo>
<server ip="x.x.x.x" name="host1" />
<server ip="x.x.x.x" name="host2" />
</serverinfo>

然后获取这样的输出配置文件

and then get output configuration file like this

[server.ini]

[server1]
server_IP = x.x.x.x
server_name = host1

[server2]
server_IP = x.x.x.x
server_name = host2

我有几个问题:


  • 是否有任何开源配置生成器程序? (可能是关键字),我想知道设计中是否可以添加/修改任何东西。

  • Is there any open source configuration generator program? (what could be the keyword), I wonder if there's anything can be added/modified in the design.

Python是否具有良好的XML解析器模块?

Does Python have good XML parser module?

使用XML文件保存原始设置好吗?我一直在考虑使用Excel,因为它维护起来更直观,但程序解析起来更困难。不知道人们如何处理。

Is it good idea to use XML file to save the original settings? I've been thinking to use Excel since it's more intuitive to maintain, but harder for program to parse. Not sure how people deal with this.

希望社区可以给我一些建议。非常感谢!

Hope the community can give me some suggestions. Thanks a lot!

编辑:
在这种情况下,有许多输出ini文件。我在谈论两件事。

In scenario that there are dozens of these output ini files. I am concerning 2 things.


  1. 有数十个ip /主机名和相关信息,可能需要人工管理,因此XML格式会有些不便。管理这些信息的最方便的结构是什么? (Excel将是用于批量修改和查找信息的便捷工具)

  1. there are dozens of ip/hostname and related informations, that may requires to be managed by human, so XML format would be a bit inconvenient. What could be the most convenient structure to manage those information? (Excel would be a handy tool to batch modify and look up info)

如果需要在ini文件中添加一些额外的行,我需要一个高效的工具只需修改模板文件并将额外的信息添加到源文件(可以是Excel文件或其他文件)中,就可以快速生成整批ini文件。

In case of need to add some extra line into ini files, I need a efficient way by just modify the template file and add extra info into the source file (may be the Excel file or whatever), then whole bunches of ini files can be generated quickly.


推荐答案

我建议使用出色的 ConfigObj 库。它甚至可以嵌套部分来读取/写入配置文件。

I recommend using excellent ConfigObj library by Michael Foord. It can read/write configuration files, even with nested sections.

这篇关于Python配置文件生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆