pickle调用
1、新建文件
root@kali:~/python/pickle# ls
fuc.py
root@kali:~/python/pickle# vi acc.py
root@kali:~/python/pickle# cat acc.py
account_info = {'0123456789':['xwb123456',15000,12000],
'9876543210':['xwb654321',20000,18000],
}
root@kali:~/python/pickle# ls
acc.py fuc.py
root@kali:~/python/pickle#
root@kali:~/python/pickle# python
Python 2.7.3 (default, Mar 14 2014, 11:57:14)
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> acc.account_info
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'acc' is not defined
SyntaxError: invalid syntax
>>> import acc #引入acc.py文件
>>> acc.account_info #查看文件内的信息
{'9876543210': ['xwb654321', 20000, 18000], '0123456789': ['xwb123456', 15000, 12000]}
>>> acc.account_info['9876543210']#查看账户9876543210内的信息
['xwb654321', 20000, 18000]
>>> acc.account_info['9876543210'][1]#查看账户9876543210内的第二个元素的值
20000
>>> acc.account_info['9876543210'][1] = 17000 #修改账户9876543210内的第二个元素的值
>>> acc.account_info#查看被修改的元素的值
{'9876543210': ['xwb654321', 17000, 18000], '0123456789': ['xwb123456', 15000, 12000]}
>>> f = open('acc.log','w')#新建一个新文件,使用覆盖写入方式打开
>>> f.write(acc.account_info)#写入信息,提示不是字符信息,无法写入
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: expected a character buffer object
>>> s = '%s' % acc.account_info #把acc.account_info信息转换程字符,进行赋值给s
>>> s
"{'9876543210': ['xwb654321', 17000, 18000], '0123456789': ['xwb123456', 15000, 12000]}"
>>> f.write(s)#成功写入信息
>>> f.close()
>>> exit()
root@kali:~/python/pickle# more acc.log
{'9876543210': ['xwb654321', 17000, 18000], '0123456789': ['xwb123456', 15000, 12000]}
root@kali:~/python/pickle#
2、使用pickle模块操作文件
root@kali:~/python/pickle# ls
acc_dump.py acc.log acc.py acc.pyc fuc.py
root@kali:~/python/pickle#
root@kali:~/python/pickle# cat acc_dump.py
#!/usr/bin/python
#---*-- coding:utf-8 --*--
import pickle#导入pickle模块
#定义字典信息
account_info = {'0123456789':['xwb123456',15000,12000],
'9876543210':['xwb654321',20000,18000],
}
f =open('pickle_file1.pkl','wb')#使用二进制覆盖写入方式打开pickle_file1.pkl
pickle.dump(account_info,f)#使用dump方式把account_info的信息放到pickle_file1.pkl文件中
f.close()
root@kali:~/python/pickle#
root@kali:~/python/pickle# cat acc.py
account_info = {'0123456789':['xwb123456',15000,12000],
'9876543210':['xwb654321',20000,18000],
}
root@kali:~/python/pickle#
root@kali:~/python/pickle#
root@kali:~/python/pickle# ls
acc.log acc.py acc.pyc fuc.py
root@kali:~/python/pickle# vi acc_dump.py
root@kali:~/python/pickle# python acc_dump.py
root@kali:~/python/pickle# ls
acc_dump.py acc.log acc.py acc.pyc fuc.py pickle_file1.pkl
root@kali:~/python/pickle# cat pickle_file1.pkl
(dp0
S'9876543210'
p1
(lp2
S'xwb654321'
p3
aI20000
aI18000
asS'0123456789'
p4
(lp5
S'xwb123456'
p6
aI15000
aI12000
as.root@kali:~/python/pickle#
3、可以避免pickle序列化重复多次写入问题
root@kali:~/python/pickle# ls
acc_dump.py acc_load.py acc.log acc.py acc.pyc fuc.py pickle_file1.pkl
root@kali:~/python/pickle#
root@kali:~/python/pickle# cat pickle_file1.pkl
(dp0
S'9876543210'
p1
(lp2
S'xwb654321'
p3
aI3777
aI18000
asS'0123456789'
p4
(lp5
S'xwb123456'
p6
aI15000
aI12000
as.root@kali:~/python/pickle# python acc_load.py
{'9876543210': ['xwb654321', 3777, 18000], '0123456789': ['xwb123456', 15000, 12000]}
{'9876543210': ['xwb654321', 8777, 8777], '0123456789': ['xwb123456', 15000, 12000]}
root@kali:~/python/pickle# cat pickle_file1.pkl
(dp0
S'9876543210'
p1
(lp2
S'xwb654321'
p3
aI8777
aI8777
asS'0123456789'
p4
(lp5
S'xwb123456'
p6
aI15000
aI12000
as.root@kali:~/python/pickle# cat acc_load.py
root@kali:~/python/pickle# cat acc_load.py
#!/usr/bin/python
# --*-- coding:utf-8 --*--
import pickle
#可以避免pickle文件重复写读出现问题的处理方式:1、是先只读打开,取完信息,关闭文件2、在写入方式打开文件操作
f = open('pickle_file1.pkl','r')# 使用只读方式打开
account_info = pickle.load(f)#使用pickle的load方式把pickle_file1.pkl导入account_info中
print account_info#打印赋值前的account_info
f.close()
account_info['9876543210'][1] = 8777#把account_info中账户9876543210的第二个值赋值为1777
account_info['9876543210'][2] = 8777#把account_info中账户9876543210的第三个值赋值为1777
print account_info#打印赋值后的account_info
f = open('pickle_file1.pkl','w')#2、覆盖式写入文件信息
pickle.dump(account_info,f)#重新dump回到文件中
f.close()
root@kali:~/python/pickle#