栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将列表转储到pickle文件中,以后再取回

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将列表转储到pickle文件中,以后再取回

腌制将序列化您的列表(将其转换为一个唯一的字节字符串,并将其输入),因此您可以将其保存到磁盘。您还可以使用pickle检索原始列表,从保存的文件中加载。

因此,首先建立一个列表,然后使用

pickle.dump
将其发送到文件…

Python 3.4.1 (default, May 21 2014, 12:39:51) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> mylist = ['I wish to complain about this parrot what I purchased not half an hour ago from this very boutique.', "Oh yes, the, uh, the Norwegian Blue...What's,uh...What's wrong with it?", "I'll tell you what's wrong with it, my lad. 'E's dead, that's what's wrong with it!", "No, no, 'e's uh,...he's resting."]>>> >>> import pickle>>> >>> with open('parrot.pkl', 'wb') as f:...   pickle.dump(mylist, f)... >>>

然后退出,然后再回来…并打开

pickle.load

Python 3.4.1 (default, May 21 2014, 12:39:51) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import pickle>>> with open('parrot.pkl', 'rb') as f:...   mynewlist = pickle.load(f)... >>> mynewlist['I wish to complain about this parrot what I purchased not half an hour ago from this very boutique.', "Oh yes, the, uh, the Norwegian Blue...What's,uh...What's wrong with it?", "I'll tell you what's wrong with it, my lad. 'E's dead, that's what's wrong with it!", "No, no, 'e's uh,...he's resting."]>>>


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/624386.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号