Py学习  »  Python

9岁小朋友做Python副业?500元:制作本地Excel的查询与生成的程序

蚂蚁学Python • 1 年前 • 254 次点击  

说明:这个单子是被学员之前做完的,9岁小朋友是自己尝试做,结果后来做完了,效果还不错。

视频演示:


前言

大家好,我是Larry,今年9岁。我是一名不折不扣的“键盘侠”,自从两年前遇上了Python这位亲切强大而有趣的新朋友后,它成为了我成长中很重要的小伙伴。说它亲切,是因为在众多的计算机语言里,它是最通俗友好易上手的;但也不影响它功能丰富、应用场景广且更迭迅速自我强化的优秀;说它有趣,是因为它既可以让我遨游创作设计的海洋,同时也拥有缜密系统的内在思维逻辑锻炼着我。随着学习蚂蚁老师课程的日渐深入,我了解到Python我这位好朋友能应用于五大重要场景,包括网络爬虫、数据分析、办公自动化、网页开发和人工智能机器学习。正好我学到了蚂蚁老师关于Pandas数据分析的课,虽然我对Excel等办公工具不太熟悉,但通过持续的学习,我尝试用我懂的Python知识去解决以下关于制作本地Excel的查询与生成的程序。

第1步:开始

1.1 查看题目

首先,打開需求就可以看到:

Excel预览图片

1.2 导入模块并读取Excel文件

等会要用的模块有:pandas、os、xlwt和uuid
用import导入的代码:

import pandas, os, xlwt, uuid

导入好后,就要读取Excel文件了。读取Excel要用到pandas的read_excel函数。

try:
    exl = pandas.read_excel(aim_path)
except:
    print('找不到文件!请检查一下文件路径或文件是否存在')
    os._exit(0)

刚刚导入os模块就是为了做异常捕获找不到文件时的退出。

第2步:查询

2.1 Excel的索引与输入

为了方便后面查询,要把DataFrame的索引(index)设为查询输入的卡号。接着,输出以卡号为索引的DF,以便用户查询。最后,就开始循环输入了。

exl.set_index('卡号', inplace = True)
print(f'{exl}\n')
while 1:
    try:
        idx = input('卡号(输入“退出”即可退出):')
        if idx == '退出':
            os._exit(0)

2.2 开始查询、丰富程序

查询用dataframe.loc[index]来完成,最后输出返回的Series。为了避免用户输入非卡号信息,就又加了异常捕获。

        res = exl.loc[idx]
        print(f'\n{res}\n')
    except KeyError:
        print('你的卡号可能输错了!我找不到这个卡号的人哦~\n')
        continue
    except:
        print('有些错误发生了!\n')
        continue

第3步:追加查询结果到Excel

3.1 读取或新建Excel

3.1.1 读取

读取跟上面一样,用read_excel

    try:
        res_exl = pandas.read_excel(res_path)
3.1.2 新建Workbook和Sheet

现在轮到xlwt模块大展身手啦~ 用Workbook函数来新建Workbook;用add_sheet函数新增Sheet

    except:
        workbook = xlwt.Workbook()
        sheet = workbook.add_sheet('new')
        col = 0
3.1.2 写入Column

在Column的位置,需要填入查询的Excel的列索引,用

list(pandas.read_excel(aim_path).columns.values)

可以获取到。然后把列索引以xlwt.write填进去,最后把DF保存再读取这个Excel。

for i in list(pandas.read_excel(aim_path).columns.values):
            sheet.write(0, col, i)
            col += 1
        workbook.save(res_path)
        res_exl = pandas.read_excel(res_path)

3.2 追加结果

首先,把结果res变量设置成列表类型。然后,在这个列表里面新增结果没有的卡号。最后把这个列表设置成一个Series(索引为查询的Excel的列索引)。

    res_series_data = list(res)
    res_series_data.insert(2, idx)
    res_series = pandas.Series(
        res_series_data, 
        index = list(
            pandas.read_excel(aim_path).columns.values
        )
    )

现在建好了Series,准备追加了。追加完后还要保存这个Excel。

    res_exl.loc[str(uuid.uuid1())] = res_series
    try:
        res_exl.to_excel(res_path, index = False)
    except:
        print('写入失败')

这里用了uuid.uuid1来随机产生索引,避免重复而修改其它人的值。最后几行就是保存的操作,python index = False的意思就是把索引隐藏掉了。

完整代码

try:
    exl = pandas.read_excel(aim_path)
except:
    print('找不到文件!请检查一下文件路径或文件是否存在')
    os._exit(0)
exl.set_index('卡号', inplace = True)
print(f'{exl}\n')
while 1:
    try:
        idx = input('卡号(输入“退出”即可退出):')
        if idx == '退出':
            os._exit(0)
        res = exl.loc[idx]
        print(f'\n{res}\n')
    except KeyError:
        print('你的卡号可能输错了!我找不到这个卡号的人哦~\n')
        continue
    except:
        print('有些错误发生了!\n')
        continue

    try:
        res_exl = pandas.read_excel(res_path)
    except:
        workbook = xlwt.Workbook()
        sheet = workbook.add_sheet('new')
        col = 0
        for i in list(pandas.read_excel(aim_path).columns.values):
            sheet.write(0, col, i)
            col += 1
        workbook.save(res_path)
        res_exl = pandas.read_excel(res_path)
    res_series_data = list(res)
    res_series_data.insert(2, idx)
    res_series = pandas.Series(
        res_series_data, 
        index = list(
            pandas.read_excel(aim_path).columns.values
        )
    )
    res_exl.loc[str(uuid.uuid1())] = res_series
    try:
        res_exl.to_excel(res_path, index = False)
    except:
        print('写入失败')

这就是我这次的全部技术总结,谢谢蚂蚁老师提供一个这么好的平台让我能跟同样喜欢Python的朋友们交流分享!


今晚来蚂蚁老师抖音直播间,618有惊喜!!!


Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/135997
 
254 次点击