|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import pickle
- func_dict = {1:'查询联系人资料', 2:'插入新的联系人', 3:'删除已有联系人', 4:'退出通讯录程序'}
- save_data_dict = dict()
- decide_code = int(input('请输入指令:'))
- if decide_code == 2:
- input_name = input('请输入联系人姓名:')
- input_tel = input('请输入用户联系电话:')
- save_data_dict[input_name] = input_tel
- file = open('save_dict.pkl', 'ab')
- pickle.dump(save_data_dict, file)
- file.close()
- elif decide_code == 1:
- input_name = input('请输入联系人姓名:')
- file = open('save_dict.pkl', 'rb')
- save_data_dict1 = pickle.load(file)
- print(save_data_dict1)
- for i in save_data_dict1.keys():
- #print(i)
- print(input_name)
- if input_name == i:
- #print(input_name)
- export_value = save_data_dict1[i]
- print(export_value)
- else:
- print('kkk')
-
复制代码
我新保存的内容会覆盖掉之前保存的,pickle有没有'wb'之外的打开模式啊?就是把信息一致追加保存的模式。
由于pickle的文件是结构性数据,直接追加是有问题的。建议先读取,再加上新数据,然后重新写入。
|
|