[英]Parsing Erlang Config file with Python
我想在python中解析一个erlang配置文件。 有它的模块吗? 此配置文件包含;
[{webmachine, [
{bind_address, "12.34.56.78"},
{port, 12345},
{document_root, "foo/bar"}
]}].
未经测试,有点粗糙,但“适用”你的例子
import re
from ast import literal_eval
input_string = """
[{webmachine, [
{bind_address, "12.34.56.78"},
{port, 12345},
{document_root, "foo/bar"}
]}]
"""
# make string somewhat more compatible with Python syntax:
compat = re.sub('([a-zA-Z].*?),', r'"\1":', input_string)
# evaluate as literal, see what we get
res = literal_eval(compat)
[{'webmachine': [{'bind_address': '12.34.56.78'}, {'port': 12345},
{'document_root': 'foo/bar'}]}]
然后,您可以将字典列表“汇总”为一个简单的dict
,例如:
dict(d.items()[0] for d in res[0]['webmachine'])
{'bind_address': '12.34.56.78', 'port': 12345, 'document_root':
'foo/bar'}
您可以使用etf库来解析python中的erlang术语https://github.com/machinezone/python_etf
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.