首页 > 代码库 > 2017-6-16 周末作业

2017-6-16 周末作业

import os                                                       #调用os,time模块
import time

def file_oper():                                                        #定义file_oper()对文件进行操作
    with open(‘staff_table.txt‘,‘r‘,encoding=‘utf-8‘) as f:              #打开文件,生成两个列表
        file_opers = []
        file_oper1 = []
        for line in f:
            file_opers.append(line.strip().split(‘,‘))
            file_oper1.append(line)
        file_opera = [file_opers,file_oper1]                             #将两个列表添加到list中
        return  file_opera                                              #返回list的值

def list():                                                             #定义list函数
    file_opers = file_oper()[0]                                         #调用file_oper函数,将file_opers赋值
    index = []                                                          #定义空列表
    staffids = []
    names = []
    ages = []
    phones = []
    depts = []
    enroll_datas = []
    for (v,i) in enumerate(file_opers):                                #获取下标
        index.append(v)                                                 #将下标添加到index列表中
    for v in index:                                                     #将每个列表中下标相同的元素放到一个列表中
        staffids.append(file_opers[v][0])
        names.append(file_opers[v][1])
        ages.append(file_opers[v][2])
        phones.append(file_opers[v][3])
        depts.append(file_opers[v][4])
        enroll_datas.append(file_opers[v][5])
    value =  http://www.mamicode.com/[staffids,names,ages,phones,depts,enroll_datas]           #将所有列表添加到新的列表value"Market" WHERE where dept = "IT"‘,            ‘查:select name,age from staff_table where age > 22,            \nselect  * from staff_table where dept = "IT",            \nselect  * from staff_table where enroll_date like "2013"‘]
    print(‘--------操作语句示例---------‘)
    for i in oper:
        print(i)
    print(‘-----------------------------‘)

def rename(func):                                                        #定义rename装饰器,对操作的文件进行备份
    def wapper(*args,**kwargs):
        func(*args,**kwargs)
        os.rename(‘message.txt‘, ‘message_%s.bak‘ % time.strftime(‘%Y%m%d%H%M%S‘))
        os.rename(‘message_1.txt‘, ‘message.txt‘)
    return wapper

@rename                                                                    #调用装饰器
def create(str1):                                                          #定义create函数
    phones = list()[3]                                                      #调用函数并赋值
    staff_id = list()[0]
    list1 = file_oper()[1]
    createdata = http://www.mamicode.com/str1.split(‘ ‘)                                            #对输入语句进行split操作,取得需要的phone值"‘)
    with open(‘message_1.txt‘, ‘w+‘, encoding=‘utf-8‘) as up_file:     #打开文件
        if update_datas[3] in depts:                                        #判断dept是否depts列表中
            for i in file_opers:                                            #存在修改
                if update_datas[3] in i:
                    i[i.index(update_datas[3])] = update_datas[1]
            i = ",".join(i)
            up_file.write(str(i) + "\n")
            print(‘------------------------------‘)
            print(‘更改成功!‘)
        else:                                                               #不存在打印提示
            for i in list1:
                up_file.write(i)
            print(‘所输入的值不在文件中!‘)

def select(str1):                                                           #定义select函数
    list1 = file_oper()[0]                                                  #调用函数并赋值
    choise = str1                                                           #将输入的字符串进行split操作,取得需要的值
    choise1 = choise.split(‘where‘)
    value = http://www.mamicode.com/choise1[-1].strip()">")
        age = choise_age[-1].strip()
        print(‘---------------------‘)
        for i in list1:
            if int(age) < int(i[2]) :
                i = ",".join(i)
                print(i)
        print(‘---------------------‘)
        print(‘以上为查找到的信息!‘)
    if value.startswith(‘dept‘):                                        #输入的值为dept则查找文件中对应的dept信息
        choise_dept = value.split(‘"‘)
        print(‘---------------------‘)
        for i in list1:
            if choise_dept[1] in i:
                i = ",".join(i)
                print(i)
        print(‘---------------------‘)
        print(‘以上为查找到的信息!‘)
    if value.startswith(‘enroll_date‘):                              #输入的值enroll_date为则查找文件中对应的信息
        choise_enroll = value.split(‘"‘)
        print(‘---------------------‘)
        for i in list1:
            if i[-1].startswith(choise_enroll[1]) :
                i = ",".join(i)
                print(i)
        print(‘---------------------‘)
        print(‘以上为查找到的信息!‘)

def mains():                                                       #定义mains函数
    example()                                                       #打印示例
    while True:
        title()                                                     #打印可操作语句
        choise = input(‘请输入操作语句(按q退出):‘)                #请用户输入语句
        if choise.startswith(‘select‘):                             #根据开头输入单词,调用对应的函数,输入q为退出
            select(choise)
        if choise.startswith(‘update‘):
            update(choise)
        if choise.startswith(‘delete‘):
            delete(choise)
        if choise.startswith(‘create‘):
            create(choise)
        if choise == ‘q‘:
            print(‘退出成功!‘)
            break

mains()                                                              #调用mains函数

  

2017-6-16 周末作业