pickel

import os,pickle
#python3.4,,,,jk409于2014-05-08用以练习pickel编写的人员信息脚本
def init():
    data=[["jk409",'25',"1988-05-02","IT"],["kkk",'20',"1988-10-07","IT","888888"]]
    f=open("./test.txt",'wb')
    pickle.dump(data,f)
    f.close()
    

def f_write():
    #添加信息
    data=[]
    xm=input("请输入您的[姓名]:")
    ss=input("请输入您的[年龄]:")
    sr=input("请输入您的[出生年月日]:")
    bm=input("请输入您的[所在部门]:")
    tel=input("请输入您的[联系电话]:")
    f=open("./test.txt",'rb')
    #f.flush()
    data=pickle.load(f)
    data.append([xm,ss,sr,bm,tel])
    f=open("./test.txt",'wb')
    pickle.dump(data,f)
    f.close()

def f_read():
    #读取信息
    f=open("./test.txt",'rb')
    data=pickle.load(f)
    #print (查询的信息如下:)
    cx=input("1.查询所有的信息   2.个人模糊信息查询  3.个人精确信息查询:")
    if cx == "1":
        print ("\n查询的信息如下:")
        for i in data:
            print ("姓名:"+i[0],"\t年龄:",i[1],"\t生日:"+i[2],"\t部门:"+i[3],"\t电话:"+i[4])
    if cx == "2":
        name_cx=input("请输入查询人的姓名:")
        print ("\n查询的信息如下:")
        for i in data:
            if i[0] == name_cx:
                print ("姓名:"+i[0],"\t年龄:",i[1],"\t生日:"+i[2],"\t部门:"+i[3],"\t电话:"+i[4])

    if cx == "3":
        name_cx=input("请输入查询人的姓名:")
        tel_cx=input("请输入查询人的电话:")
        print ("\n查询的信息如下:")
        for i in data:
            if i[0] == name_cx and i[4] == tel_cx:
                print ("姓名:"+i[0],"\t年龄:",i[1],"\t生日:"+i[2],"\t部门:"+i[3],"\t电话:"+i[4])
    f.close()


def kz():
    #对已有的列表进行扩展
    data_new=[]
    f=open("./test.txt",'rb')
    data=pickle.load(f)
    for i in data:
        i.extend(['888888'])
        data_new.extend([i])
    print (data_new)
    f=open("./test.txt",'wb')
    pickle.dump(data_new,f)
    f.close()


def sc():
    #定义新空列表,然后扩展新列表的元素,最后写入文件
    data_sc=[]
    f=open("./test.txt",'rb')
    data=pickle.load(f)
    #print (data)
    name_del=input("请输入需要删除的信息的[姓名]:")
    for i in data:
        if i[0] == name_del:
            print ("姓名:"+i[0],"\t年龄:",i[1],"\t生日:"+i[2],"\t部门:"+i[3],"\t电话:"+i[4])
        else:
            data_sc.extend([i])
    print(data_sc)
    f=open("./test.txt",'wb')
    pickle.dump(data_sc,f)
    f.close()



def sc2():
    #"list列表index删除"
    f=open("./test.txt",'rb')
    data=pickle.load(f)
    name_del=input("请输入需要删除的信息的test[姓名]:")
    for i in data:
        if i[0] == name_del:
            del data[data.index(i)]
    print(data)
    f=open("./test.txt",'wb')
    pickle.dump(data,f)
    f.close()



def xg():
    f=open("./test.txt",'rb')
    data=pickle.load(f)
    n_mod=input("请输入需要修改的信息的test[姓名]:")
    for i in data:
        if i[0] == n_mod:
            print(i,"\n","1.姓名 2.年龄 3.生日 4.部门 5.电话")
            bh_mod=input("请输入你修改项目的[编号1~5]:")
            if bh_mod == "1":
                n_mod=input("请输入您的新姓名:")
                data[data.index(i)][0] = n_mod

            if bh_mod == "2":
                l_mod=input("请输入您的新年龄:")
                data[data.index(i)][1] = l_mod

            if bh_mod == "3":
                s_mod=input("请输入您的新生日:")
                data[data.index(i)][2] = s_mod

            if bh_mod == "4":
                b_mod=input("请输入您的新部门:")
                data[data.index(i)][3] = b_mod

            if bh_mod == "5":
                d_mod=input("请输入您的新电话:")
                data[data.index(i)][4] = d_mod
            if bh_mod == "":
                print('-----------')
    print(data)
    f=open("./test.txt",'wb')
    pickle.dump(data,f)  
    f.close()
    
def show():
    print("---------------------------------------")
    print("1.查询   2.添加  3.修改   4.删除   5.退出 ")


if __name__=='__main__':
    while 1:
        os.system('cls')
        show()
        xz=input("请输入您的需要选项的编号[1/2/3/4/5]:")
        if xz == "1": f_read()
        if xz == "2": f_write()
        if xz == "3": xg()
        if xz == "4": sc2('4')
        if xz == "5": break
        if xz == "" or xz == " ": print("空")
        if xz == "0":
            init=input('改操作会格式所有数据,请确实是否操作[yes|no]:')
            if init == "yes":
                print("格式化中......")
                init()
                print("格式化完成!")
        xyz=input("\n按任意键继续......")

想起了以前写的蹩脚的shell版人口管理脚本

你可能感兴趣的:(python,pickel)