简体   繁体   中英

Parsing Erlang Config file with Python

I want to parse an erlang config file in python. 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(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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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