简体   繁体   English

使用Python解析Erlang配置文件

[英]Parsing Erlang Config file with Python

I want to parse an erlang config file in python. 我想在python中解析一个erlang配置文件。 Is there a module for it? 有它的模块吗? This config file contains; 此配置文件包含;

[{webmachine, [
 {bind_address, "12.34.56.78"},
 {port, 12345},
 {document_root, "foo/bar"}
]}].

Untested and a little rough, but 'works' on your example 未经测试,有点粗糙,但“适用”你的例子

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'}]}]

You could then "roll-up" the list of dictionary into a simple dict , eg: 然后,您可以将字典列表“汇总”为一个简单的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM