从文件中读取Python中的元组列表列表 [英] Read list of lists of tuples in Python from file

查看:887
本文介绍了从文件中读取Python中的元组列表列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读写文件中的元组列表.

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)]]

我用过

  • pickle.dump(g_faces, fp)
  • pickle.load(fp)
  • pickle.dump(g_faces, fp)
  • pickle.load(fp)

但是该文件不可读.有简单的方法吗?

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

推荐答案

尝试json模块.

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]

这篇关于从文件中读取Python中的元组列表列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆