【路飞学城】第二模块作业二

现要求你写⼀个简单的员⼯信息增删改查程序,需求如下:
该表在⽂件中的存储⽅式:
在这里插入图片描述

⽂件名:staff_table(⽂件内容如下)

每⼀句对应的内容:序号,姓名,年龄,⼿机号,部分,入职时间

1,Alex Li,22,13651054608,IT,2013-04-01
2,Jack Wang,28,13451024608,HR,2015-01-07
3,Rain Wang,21,13451054608,IT,2017-04-01
4,Mack Qiao,44,15653354208,Sales,2016-02-01
5,Rachel Chen,23,13351024606,IT,2013-03-16
6,Eric Liu,19,18531054602,Marketing,2012-12-01
7,Chao Zhang,21,13235324334,Administration,2011-08-08
8,Kevin Chen,22,13151054603,Sales,2013-04-01
9,Shit Wen,20,13351024602,IT,2017-07-03
10,Shanshan Du,26,13698424612,Operation,2017-07-02

作业需求:

对⽤户输⼊的字符串进⾏解析,从⽂件中筛选出相应符合条件的结果集,对文件实现增删改查

小编累了,直接贴码吧,good job

import os

staff_msg = '''1,Alex Li,22,13651054608,IT,2013-04-01
2,Jack Wang,28,13451024608,HR,2015-01-07
3,Rain Wang,21,13451054608,IT,2017-04-01
4,Mack Qiao,44,15653354208,Sales,2016-02-01
5,Rachel Chen,23,13351024606,IT,2013-03-16
6,Eric Liu,19,18531054602,Marketing,2012-12-01
7,Chao Zhang,21,13235324334,Administration,2011-08-08
8,Kevin Chen,22,13151054603,Sales,2013-04-01
9,Shit Wen,20,13351024602,IT,2017-07-03
10,Shanshan Du,26,13698424612,Operation,2017-07-02
'''
def init_staff_msg():
    '''
    初始化员工信息表
    :return:
    '''
    with open('staff_msg.txt','w') as f:
        f.writelines(staff_msg)

def print_staff_msg():
    '''
    打印出员工信息表
    :return:
    '''
    print('员工信息表'.center(50,'*'))
    with open('staff_msg.txt','r') as f:
        for line in f:
            print(line)
#TODO:查询员工信息
def inquire_msg():
    '''
    模糊查询员工信息
    :return:
    '''
    msg = '''
    请按照如下样例进行查询:
    1. find name,age from staff_table where age > 22
    2. find * from staff_table where dept = "IT"
    3. find * from staff_table where enroll_date like "2013"
    '''
    update_staff_msg = []       # 存储满足查询条件的员工信息
    while True:
        print(msg)
        user_input = input('请输入查询命令,按照上面格式正确输入,别弄错了哦,大兄弟:>>>:').strip().split(' ') # 按照空格分割,得到一个列表
        if user_input[5] == 'age' and user_input[-2] == '>':      # 按照条件1查询
            with open('staff_msg.txt','r') as f:
                for line in f:
                    staff_msg_list = line.strip().split(',')        # 将字符串按照','分割成一个列表
                    if staff_msg_list[2] > user_input[7]:
                        update_staff_msg.append(staff_msg_list)     # 将满足条件的员工信息加入到列表
            break
        elif user_input[5] == 'age' and user_input[-2] == '<':  # 按照条件1查询
            with open('staff_msg.txt', 'r') as f:
                for line in f:
                    staff_msg_list = line.strip().split(',')  # 将字符串按照','分割成一个列表
                    if staff_msg_list[2] < user_input[7]:
                        update_staff_msg.append(staff_msg_list)  # 将满足条件的员工信息加入到列表
            break
        elif user_input[5] == 'age' and user_input[-2] == '=':  # 按照条件1查询
            with open('staff_msg.txt', 'r') as f:
                for line in f:
                    staff_msg_list = line.strip().split(',')  # 将字符串按照','分割成一个列表
                    if staff_msg_list[2] == user_input[7]:
                        update_staff_msg.append(staff_msg_list)  # 将满足条件的员工信息加入到列表
            break
        elif user_input[5] == 'dept':   # 按照条件2查询
            with open('staff_msg.txt','r') as f:
                for line in f:
                    staff_msg_list = line.strip().split(',')
                    if staff_msg_list[-2] == eval(user_input[7]):
                        update_staff_msg.append(staff_msg_list)
            break
        elif user_input[5] == 'enroll_date':    # 按照条件3查询
            with open('staff_msg.txt','r') as f:
                for line in f:
                    staff_msg_list = line.strip().split(',')
                    if staff_msg_list[5].split('-')[0] == eval(user_input[7]):
                        update_staff_msg.append(staff_msg_list)
            break
        else:
            print("输入错误,请重新输入。")
    print("满足查询条件的员工信息如下,人数为:\033[31;1m%d\033[0m。" % len(update_staff_msg))
    [print(update_staff_msg[i]) for i in range(len(update_staff_msg))]
#TODO:添加新员工信息
def add_new_staff():
    '''
    创建新员工记录
    :return:
    '''
    msg = '''
    请按照如下样例创建新员工记录:
    add staff_table Alex Li,25,134435344,IT,2015-10-29
    '''
    while True:
        print(msg)
        user_input = input("请输入查询命令,按照上面格式正确输入,别弄错了哦,大兄弟>>>:").strip().split(',')
        if user_input[0].split(' ')[0] == 'add':        # 输入格式正确
            user_input[0] = ' '.join(user_input[0].split(' ')[2:])
            with open('staff_msg.txt','r+') as f:
                staff_phone = []                # 存储员工信息的电话号码phone
                for line in f:
                    staff_phone.append(line.strip().split(',')[3])
                if user_input[2] in staff_phone:
                    print("这条记录已经存在。不能重复插入信息!!!")
                else:
                    new_staff_index = str(len(staff_phone) + 1)         # 得到新添加员工的索引
                    user_input.insert(0,new_staff_index)                # 将索引值插入到最前面
                    user_input = ','.join(user_input)                   # 列表-->字符串
                    f.write(user_input)
                    f.write('\n')
                    print("新员工记录添加完成。")
                    break
        else:
            print("输入错误,请重新输入。")

#TODO:删除相关员工信息
def delete_staff():
    '''
    删除员工信息
    :return:
    '''
    msg = '''
    请按照如下样例删除员工信息:
    del from staff where id=3
    '''
    while True:
        print(msg)
        user_input = input('请输入查询命令,按照上面格式正确输入,别弄错了哦,大兄弟>>>:').strip().split(' ')
        if user_input[0] == 'del':
            file_old = open('staff_msg.txt','r+')
            file_new = open('staff_msg_new.txt','w')
            for line in file_old:
                line_split = line.strip().split(',')
                if user_input[4].split('=')[1] != line_split[0]:    # 不符合条件的员工信息保存下来,写入新的文件
                    file_new.write(line)
            file_new.close()
            file_old.close()
            os.remove('staff_msg.txt')          # 删除旧文件
            os.rename('staff_msg_new.txt','staff_msg.txt')      # 重命名新文件
            print("员工信息删除成功。")
            break
        else:
            print("输入错误,请重新输入")
#TODO:更新员工信息
def update_staff_msg():
    '''
    更新员工信息
    :return:
    '''
    msg = '''
    请按照如下样例更新员工信息:
    UPDATE staff_table SET dept="Market" WHERE dept="IT"
    UPDATE staff_table SET age=25 WHERE name="Alex Li"
    '''
    while True:
        print(msg)
        user_input_raw = input('请输入查询命令,按照上面格式正确输入,别弄错了哦,大兄弟>>>:').strip()
        user_input = user_input_raw.split(' ')
        if user_input[0].lower() == 'update':
            count = 0       # 计数,记录的改动次数
            file_old = open('staff_msg.txt','r+')
            file_new = open('staff_msg_new.txt','w+')
            update_msg_new = []             # 存储更新后的员工信息
            update_msg_old = []             # 存储更新前的员工信息
            for line in file_old:
                line = line.strip().split(',')          # 字符串-->列表
                if user_input[5].split('=')[0] == 'dept':   # 按照样例1更新员工信息
                    if line[4] == eval(user_input[5].split('=')[1]):
                        update_msg_old.append(','.join(line))
                        line[4] = eval(user_input[3].split('=')[1])
                        line = ','.join(line)
                        count+=1
                        update_msg_new.append(line)
                    else:
                        line = ','.join(line)

                elif user_input[5].split('=')[0] == 'name': # 按照样例2更新员工信息
                    if line[1] ==  eval(user_input_raw.split('=')[2]):
                        update_msg_old.append(','.join(line))
                        line[2] = user_input[3].split('=')[1]
                        line = ','.join(line)
                        count+=1
                        update_msg_new.append(line)
                    else:
                        line = ','.join(line)

                else:
                    pass
                file_new.write(line)
                file_new.write('\n')
            print("您总共修改了\033[31;1m%d\033[0m条记录" % count)
            print("修改前的\033[31;1m%d\033[0m个员工信息" % count)
            [print(update_msg_old[i]) for i in range(len(update_msg_old))]
            print("修改后的\033[31;1m%d\033[0m个员工信息" % count)
            [print(update_msg_new[i]) for i in range(len(update_msg_new))]

            file_old.close()
            file_new.close()
            os.remove('staff_msg.txt')
            os.rename('staff_msg_new.txt','staff_msg.txt')
            break
        else:
            print("输入错误,请重新输入。")

def main():
    '''
    主函数
    :return:
    '''
    msg = '''
    1、查询员工信息
    2、添加新员工信息
    3、删除员工信息
    4、更改员工信息
    5、打印所有员工信息
    6、退出程序
    '''
    init_staff_msg()
    while True:
        print(msg)
        user_choose = input("请输入你的选择>>>")
        if user_choose == '1':
            inquire_msg()
        elif user_choose == '2':
            add_new_staff()
        elif user_choose == '3':
            delete_staff()
        elif user_choose == '4':
            update_staff_msg()
        elif user_choose == '5':
            print_staff_msg()
        elif user_choose == '6':
            exit("程序结束!!!")
        else:
            print("您的选择有误,请重新输入")

if __name__ == '__main__':
    main()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
爬虫(Web Crawler)是一种自动化程序,用于从互联网上收集信息。其主要功能是访问网页、提取数据并存储,以便后续分析或展示。爬虫通常由搜索引擎、数据挖掘工具、监测系统等应用于网络数据抓取的场景。 爬虫的工作流程包括以下几个关键步骤: URL收集: 爬虫从一个或多个初始URL开始,递归或迭代地发现新的URL,构建一个URL队列。这些URL可以通过链接分析、站点地图、搜索引擎等方式获取。 请求网页: 爬虫使用HTTP或其他协议向目标URL发起请求,获取网页的HTML内容。这通常通过HTTP请求库实现,如Python中的Requests库。 解析内容: 爬虫对获取的HTML进行解析,提取有用的信息。常用的解析工具有正则表达式、XPath、Beautiful Soup等。这些工具帮助爬虫定位和提取目标数据,如文本、图片、链接等。 数据存储: 爬虫将提取的数据存储到数据库、文件或其他存储介质中,以备后续分析或展示。常用的存储形式包括关系型数据库、NoSQL数据库、JSON文件等。 遵守规则: 为避免对网站造成过大负担或触发反爬虫机制,爬虫需要遵守网站的robots.txt协议,限制访问频率和深度,并模拟人类访问行为,如设置User-Agent。 反爬虫应对: 由于爬虫的存在,一些网站采取了反爬虫措施,如验证码、IP封锁等。爬虫工程师需要设计相应的策略来应对这些挑战。 爬虫在各个领域都有广泛的应用,包括搜索引擎索引、数据挖掘、价格监测、新闻聚合等。然而,使用爬虫需要遵守法律和伦理规范,尊重网站的使用政策,并确保对被访问网站的服务器负责。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Recently 祝祝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值