如何挑选清单?

Lew*_*wis 44 python list pickle

我正在尝试保存一个列表,只包含字符串,以便以后可以访问.有人告诉我要用酸洗.我希望得到一个例子,并了解酸洗是什么.

Mik*_*rns 89

Pickling将序列化您的列表(将其转换,并将其条目转换为唯一的字节字符串),因此您可以将其保存到磁盘.您还可以使用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 darwin
Type "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)
... 
>>> 
Run Code Online (Sandbox Code Playgroud)

然后退出并稍后回来......并打开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 darwin
Type "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."]
>>>
Run Code Online (Sandbox Code Playgroud)

  • 这是一个沉重的故事,在那个字符串列表迈克......无论如何,谢谢你! (28认同)