python:关于cpickle模块
永爱果果 2009-12-08 09:30:23 我用cpickle模块的dump函数向文本文件添加内容,发现添加的不是我想要的:
代码如下:
#!/usr/bin/python
# Filename: pickling.py
import cPickle as p
#import pickle as p
shoplistfile = 'shoplist.txt'
# the name of the file where we will store the object
shoplist = ['apple', 'mango', 'carrot']
# Write to the file
f = file(shoplistfile, 'w')
p.dump(shoplist, f) # dump the object to a file
f.close()
我想让shoplist.txt的内容为:
apple mango carrot
结果用此程序运行后,shoplist.txt的内容为:
(lp1
S'apple'
p2
aS'mango'
p3
aS'carrot'
p4
a.
难道我想向文件插入内容只能用file来操作,不能用容器吗(cpickle)
另外如果使用cpickle的dump函数还插入一个已有文件会不会吧原来文件的内容覆盖啊还是在源文件的末尾插入啊
谢谢,刚学习,试着编个程序,遇到麻烦了,请指点迷津,不胜感激