简体   繁体   English

如何在Python中将配置文件转换为dict?

[英]How do I convert config file to dict in Python?

server 192.168.10.10 443 {
    protocol TCP

    _server 192.168.10.1 443 {
        weight 1
        SSL_GET {
            url {
                path /
            }
            connect_timeout 3
        }
    }
}

Have you got a full specification for the configuration files? 您是否有配置文件的完整规范?

I am going to guess that what you posted is an example of one, however it's very easy to produce a naive parser that works on one example but not others using different features. 我猜想您发布的内容只是其中的一个示例,但是很容易生成一个可以在一个示例上运行但不能在其他示例上使用不同功能的幼稚解析器。

Also the dictionary structure that it is useful to get out of your parser depends very much on what you wish to use it for. 同样,退出解析器很有用的字典结构在很大程度上取决于您希望将其用于什么。

If you find out what standard this config file goes from you may also find that someone has done the hard work for you. 如果您发现此配置文件的标准,您可能还会发现有人为您完成了艰苦的工作。

If you don't have one already you're going to have to create a grammar and parse that into a Python object. 如果您还没有一个语法,则必须创建一个语法并将其解析为Python对象。 If you've never done this before, you're in for a lot of reading to get up to par! 如果您从未做过此事,那么您将需要大量阅读才能达到标准!

My first suggestion to solve this would be to check out PyParsing . 解决这个问题的第一个建议是签出PyParsing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:如何将列表写入文件,然后将其拉回 memory(dict 表示为字符串转换为 dict)? - Python: How do I write a list to file and then pull it back into memory (dict represented as a string convert to dict) later? 如何在Python中将此字符串转换为字典? - How do I convert this string into a dict in python? 在 python - 我如何将项目转换为字典? - In python - how do I convert items to dict? 以下是有效的json吗? 我如何将它转换为python中的dict - is the following a valid json ? how do I convert it into a dict in python 将文件转换为python dict - convert file to python dict 如何使用 json 模块将 python 对象转换为 (json) 嵌套 dict,而不创建类似文件的对象? - How do I convert a python object to (json) nested dict using the json module, without making a file-like object? 将 dict 转换为配置文件,然后从配置中恢复 dict - Convert dict into a config file then restore the dict from config 如何将字典/嵌套字典的字典转换为列表字典 - How do I convert dict of dicts/nested dicts into dict of list 如何将 OrderedDict 转换为 Dict - How can I do to convert OrderedDict to Dict 我应该如何将此字符串转换为 python 中的 dict? - How should i convert this string into dict in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM