问题
I'd like to read and write a list of lists of tuples from and to files.
g_faces = [[(3,2)(3,5)],[(2,4)(1,3)(1,3)],[(1,2),(3,4),(6,7)]]
I used
pickle.dump(g_faces, fp)
pickle.load(fp)
But the file is not human readable. Is there an easy way to do it?
回答1:
Try the json module.
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]
来源:https://stackoverflow.com/questions/40074923/read-list-of-lists-of-tuples-in-python-from-file