[英]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.