Read list of lists of tuples in Python from file
<p>I'd like to read and write a list of lists of tuples from and to files.</p>

<pre><code>g_faces = [[(3,2)(3,5)],[(2,4)(1,3)(1,3)],[(1,2),(3,4),(6,7)]]
</code></pre>

<p>I used</p>

<ul>
<li><code>pickle.dump(g_faces, fp)</code></li>
<li><code>pickle.load(fp)</code></li>
</ul>

<p>But the file is not human readable. Is there an easy way to do it?</p>

<p>Try the json module.</p>

<pre><code>import json

g_faces = [[(3,2), (3,5)],[(2,4), (1,3), (1,3)],[(1,2), (3,4), (6,7)]]

json.dump(g_faces, open('test.json', 'w'))

g_faces = json.load(open('test.json'))

# cast back to tuples
g_faces = [[tuple(l) for l in L] for L in g_faces]
</code></pre>

python serialization pickle