- 论坛徽章:
- 0
|
本帖最后由 warlegtree 于 2015-10-16 09:12 编辑
做了一个account.txt
123456 123456 15000
987654 123456 10000
想把这个文件读取成dict,然后进行pickle的处理
#!/usr/bin/env python
import pickle
import os
afile = file('account.txt','rb')
dict_acc = {}
for line in afile.readlines():
key = line.split()[0]
info = line.split()[1:]
dict_acc[key] = info
afile.close()
if os.path.exists('user.pkl'):
pass
else:
user_file = file('user.pkl','wb')
pickle.dump(afile, user_file)
user_file.close()
但执行不成功,报错信息如下
Traceback (most recent call last):
File "pictest.py", line 15, in <module>
pickle.dump(fa, user_file)
File "/usr/lib64/python2.6/pickle.py", line 1362, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib64/python2.6/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib64/python2.6/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/usr/lib64/python2.6/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle file objects
望高手们提点下小白,谢谢
----------------------------------------------------
找到原因了,有一行写错了,应该是pickle.dump(dict_acc,user_file)
afile的type是文件,python无法pickle文件对象 |
|