Parse text position vector to floats
<p>I have a .txt file that contains lines in this format:</p>

<pre><code>{"Position":[81.2305,4.05698,9.14912]}
</code></pre>

<p>Because I have lots of other lines that start with an open bracket and a name enclosed in quotes followed by a semi-colon, I have split the line into two like this:</p>

<pre><code>[ '{"Position":', '[81.2305,4.05698,9.14912]}' ]
</code></pre>

<p>I would like to parse the second string into a list of 3 floats. What is the best way to do this? </p>

<p>Your data looks like JSON, so you can use the built-in JSON module:</p>

<pre><code>import json

pos = [] # list of (x,y,z)

with open('t.txt') as infile:
    for line in infile:
        # skip blank lines (add other cases as needed)
        if not line.strip():
            continue

        item = json.loads(line)
        if "Position" in item:
            pos.append(item["Position"])

print(pos)
</code></pre>

python regex python-3.x