无论位置如何,如何解析配置文件中的参数?
例如,配置文件可以有
CFLAGS = "xyz"
CXXFLAGS = "xyz"
OR
CXXFLAGS = "xyz"
CFLAGS = "xyz"
我能想到的最佳实现是将参数分开并将左侧输入开关
for line in file
x = line.split("=")
switch(x[0])
case CFLAGS
do cflags
case CXXFLAGS
do cxxflags
但是比我有更多经验的人是如何做到的?我知道可能有一些开源程序可以做到这一点,但我什至不知道在哪里查找他们的源代码。 我主要使用 python 和 C 进行编程,因此两者中的实现/伪代码/whattolookup 将是首选,尽管其他语言也可以。
提前致谢。
PS 尽量避免在答案中使用任何形式的 re、regex、regexp、正则表达式或其任何派生词,除非不可避免:P。
e.g. A configuration file can have
CFLAGS = "xyz"
CXXFLAGS = "xyz"
OR
CXXFLAGS = "xyz"
CFLAGS = "xyz"
Best implementation I could think of would be to just split the argument and feed the left side into a switch
for line in file
x = line.split("=")
switch(x[0])
case CFLAGS
do cflags
case CXXFLAGS
do cxxflags
But how do people who have way more experience than me do it? I know theres probably some open source programs who do this but I wouldn't even know where to look in their source for this.
I program mainly in python and C so implementations/pseudocode/whattolookup in both would be preferred although other languages are fine also.
Thanks in advance.
P.S. try to avoid saying any form of re, regex, regexp, regular expressions, or any derivative thereof in your answers unless its unavoidable :P.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Python 中,只需使用 ConfigParser 模块即可为您解析类似 .ini 的配置文件。
自己重新实现这一点,我发现将配置数据视为一种字典很方便。这自然会转换为 Python 的字典,因此如果我将行拆分为
=
我只是继续更新:使用此方案,配置文件中键的顺序并不重要,就像字典本身并不重要 - 只要您可以查找键的值,你很高兴。
例如,如果您查看
ConfigParser
的内部结构(相关方法是_read
),您会发现这正是它的作用。这些选项保存在字典中(每个部分一个,因为.ini
配置文件具有一层层次结构)。使用正则表达式从文件中解析行,并将键、值对添加到字典中,如上所述。这是Python。在 C 中,我想有很多库可以做到这一点,但是实现你自己的库将遵循完全相同的算法。您可以对字典使用某种关联数组数据结构(哈希表、树或其他什么,并不重要),并进行相同的解析和解析。分配。
In Python just use the
ConfigParser
module which will parse.ini
-like configuration files for you.Re implementing this yourself, I find it convenient to view configuration data as a kind of dictionary. This naturally translates to Python's dicts, so if I split the line to
<key> = <value>
I just go on and update:With this scheme, the order of the keys in the configuration file doesn't matter, just like it doesn't matter in the dictionary itself - as long as you can lookup values for keys, you're happy.
If you look under the hood of
ConfigParser
, for example (the relevant method is_read
), you will find this is exactly what it does. The options are kept in a dictionary (one per section, because.ini
configuration files have one level of hierarchy). Lines are parsed from the file using regular expressions andkey, value
pairs are added to the dictionary as I described above.This is Python. In C, I imagine there are quite a few libraries for doing this, but implementing your own would follow exactly the same algorithm. You'd use some kind of associative array data structure for the dictionary (hash table, tree, or whatever, doesn't really matter) and do the same parsing & assigning.
正如Eli Bendersky 说,在 Python 中你应该只使用提供的 ConfigParser。
如果您坚持自己做,我推荐他将配置存储为字典的方法。另一种方法是将选项映射到处理值并用它们执行某些操作的函数:
显然,处理函数必须能够接受您的
option
和value
参数重新通过它。As Eli Bendersky says, in Python you should just use the provided ConfigParser.
If you insist on doing it yourself, his method of storing the configuration as a dictionary is one I recommend. Another way is to map the options to functions which process the values and do something with them:
Obviously, the handler functions must be able to accept the
option
andvalue
parameters you're passing it.