Converting .lua table to a python dictionary
<p>I have this kind of input:</p>

<pre><code>    sometable = {
        ["a"] = {
            "a1",
        },
        ["b"] = {
            "b1",
            ["b2"] = true,
        },
        ["c"] = {
            "c1",
            ["c2"] = true,
        },
    },
</code></pre>

<p>And would like to convert it to some dictionary I can work with in python - or basically, I just need to be able to read the data in this pattern:</p>

<pre><code>print sometable[b][b2]
</code></pre>

<p>What is the best solution to this? I tried to do a bunch of replaces and convert it using <code>ast</code>, ie:</p>

<pre><code>def make_dict(input): # just body, ie. without 'sometable'
    input = input.replace("=", ":")
    input = input.replace("[\"", "\"")
    input = input.replace("\"]", "\"")
    input = input.replace("\t", "")
    input = input.replace("\n", "")
    input = "{" + input + "}"
    return ast.literal_eval(input)
</code></pre>

<p>The problem is that the output is:</p>

<pre><code>{
 "a" : 
  {"a1", },
 "b" : 
  {"b1", "b2" : true,},
 "c" : 
  {"c1", "c2" : 1,},
}
</code></pre>

<p>The error (<code>invalid syntax</code>) is on <code>{"b1", "b2" : true,},</code>. Any suggestion?</p>

<p>Look at this package: <a href="https://github.com/SirAnthony/slpp" rel="nofollow">https://github.com/SirAnthony/slpp</a>.</p>

<pre><code>&gt;&gt;&gt; from slpp import slpp as lua
&gt;&gt;&gt; code = """{
    ["a"] = {
        "a1",
    },
    ["b"] = {
        "b1",
        ["b2"] = true,
    },
    ["c"] = {
        "c1",
        ["c2"] = true,
    },
}"""
&gt;&gt;&gt; print(lua.decode(code))
{'a': ['a1'], 'c': {0: 'c1', 'c2': True}, 'b': {0: 'b1', 'b2': True}}
</code></pre>

python dictionary lua