Python头歌实训之通信录csv文件管理

1.通信簿.csv文件进行查询

import pandas as pd
from datetime import datetime
df = pd.read_csv('wjgl/src/通信录.csv', names=["姓名","住址","电话","生日"])
df.to_csv('wjgl/通信录.csv', encoding="utf-8", index=0, header=0)

def xstxb():
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    for i in range(0, len(df)):
        print(df.iloc[i][:])

def cztxb():
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    x = input("")
    l = df.loc[df["姓名"] == x, ["姓名","住址","电话","生日"]]
    if l.empty:
        print("查无此人")
    else:
        print(l)

ch = ''
while ch != '0':
    ch = input()
    if ch == '1':
        zjtxb()
    elif ch == '2':
        sctxb()
    elif ch == '3':
        cztxb()
    elif ch == '4':
        xgtxb()
    elif ch == '5':
        xstxb()
    elif ch == '0':
        break

2.在通信簿csv文件中增加数据

import pandas as pd
from datetime import datetime
df = pd.read_csv('wjgl/src/通信录.csv', names=["姓名","住址","电话","生日"])
df.to_csv('wjgl/通信录.csv', encoding="utf-8", index=0, header=0)

def zjtxb():
    a = input()
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    df1 = df.loc[df['姓名'] == a]
    if df1.empty:
        b = input()
        c = input()
        d = input()
        if int(d[4:6]) not in range(1,13) or int(d[-2:]) > 30:
            print('生日格式不对')
        elif len(c) != 11:
            print('电话号码必须是11位')
        else:
            d = d[:4]+'-'+d[4:6]+'-'+d[6:]
            df.loc[120] = [a,b,c,d]
            df.to_csv('wjgl/通信录.csv',encoding='utf-8',index=0,header=0)
    else:
        print('该用户已经存在')

def xstxb():
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    for i in range(0, len(df)):
        print(df.iloc[i][:])

def cztxb():
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    x = input("")
    l = df.loc[df["姓名"] == x, ["姓名","住址","电话","生日"]]
    if l.empty:
        print("查无此人")
    else:
        print(l)

ch = ''
while ch != '0':
    ch = input()
    if ch == '1':
        zjtxb()
    elif ch == '2':
        sctxb()
    elif ch == '3':
        cztxb()
    elif ch == '4':
        xgtxb()
    elif ch == '5':
        xstxb()
    elif ch == '0':
        break

3.在通信录csv文件中删除数据

import pandas as pd
from datetime import datetime
df = pd.read_csv('wjgl/src/通信录.csv', names=["姓名","住址","电话","生日"])
df.to_csv('wjgl/通信录.csv', encoding="utf-8", index=0, header=0)

def xstxb():
    df=pd.read_csv('wjgl/通信录.csv',names=["姓名","住址","电话","生日"])
    for i in range(0,len(df)):
        print(df.iloc[i][:])

def sctxb():
    a = input('')
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    df1 = df.loc[df['姓名'] == a]
    if df1.empty:
        print('查无此人')
    else:
        n = df.loc[df['姓名']==a].index.tolist()
        df = df.drop(index=n)
        df.to_csv('wjgl/通信录.csv', encoding='utf-8', index=0, header=0)
        print('数据已删除')

def cztxb():
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    x = input("")
    l = df.loc[df["姓名"] == x, ["姓名","住址","电话","生日"]]
    if l.empty:
        print("查无此人")
    else:
        print(l)

ch = ''
while ch != '0':
    ch = input()
    if ch == '1':
        zjtxb()
    elif ch == '2':
        sctxb()
    elif ch == '3':
        cztxb()
    elif ch == '4':
        xgtxb()
    elif ch == '5':
        xstxb()
    elif ch == '0':
        break

4.在通信簿csv文件中修改数据

import pandas as pd
from datetime import datetime
df = pd.read_csv('wjgl/src/通信录.csv', names=["姓名","住址","电话","生日"])
df.to_csv('wjgl/通信录.csv', encoding="utf-8", index=0, header=0)

def xstxb():
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    for i in range(0, len(df)):
        print(df.iloc[i][:])

def cztxb():
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    x = input("")
    l = df.loc[df["姓名"] == x, ["姓名","住址","电话","生日"]]
    if l.empty:
        print("查无此人")
    else:
        print(l)

def xgtxb():
    a = input()
    df = pd.read_csv('wjgl/通信录.csv', names=["姓名","住址","电话","生日"])
    df1 = df.loc[df['姓名'] == a]
    if df1.empty==True:
        print('查无此人')
    else:
        b=input()
        c=input()
        d=input()
        if int(d[4:6]) not in range(1,13) or int(d[-2:])>30:
            print('生日格式不对')
        elif len(c) != 11:
            print('电话号码必须是11位')
        else:
            d = d[:4]+'-'+d[4:6]+'-'+d[6:]
            df.loc[df['姓名'] == a] = [a,b,c,d]
            df.to_csv('wjgl/通信录.csv', encoding='utf-8', index=0, header=0)
            print('数据已修改')

ch = ''
while ch != '0':
    ch = input()
    if ch == '1':
        zjtxb()
    elif ch == '2':
        sctxb()
    elif ch == '3':
        cztxb()
    elif ch == '4':
        xgtxb()
    elif ch == '5':
        xstxb()
    elif ch == '0':
        break

你可能感兴趣的:(头歌实训,python)