吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 931|回复: 15
收起左侧

[学习记录] python处理excle筛选学生“出”“进”大门时间

  [复制链接]
Lengxiy 发表于 2023-3-21 15:11
本帖最后由 Lengxiy 于 2023-3-22 21:55 编辑

写了一个下午,写的有点白痴尤其是关于下表越界问题......其实还是有点问题,好像写反了,不过就当第一次留个学习记录吧,后面慢慢改
业务来源:
昨天老师突然给了我两个excle  每个excle都有5w的学生进出时间以及姓名学号数据等等......
其中A列为姓名,B列为学号,H列为校门口机器判断的学生是进去还是出来,K列是进/出时间
excle我先进行了筛选和格式更改的,因为统一格式(我改为了常规格式)后可以减少其他不必要的问题
由于老师需要的是进出校门时间大于10s小于120s的同学的信息留下来,所以有需要的请自己改改代码应该就能凑合着用
文件例子地址(已经清除了铭感信息):
https://alist.zymys.cn/%E9%98%BF%E9%87%8C%E4%BA%91%E7%9B%98/%E4%B8%BE%E4%BE%8B%E5%AD%90

写的代码跟驼屎一样,我也知道,大佬勿喷,毕竟第一次做,诚心求教{:1_909:}
[Python] 纯文本查看 复制代码
import xlrd
from xlrd import xldate_as_tuple
from datetime import datetime

worksheet = xlrd.open_workbook('./new1.xlsx')
sheet_names = worksheet.sheet_names()  # 获取第一个工作表
for sheet_name in sheet_names:
    sheet = worksheet.sheet_by_name(sheet_name)
    rows = sheet.nrows  # 获取行数
    cols = sheet.ncols  # 获取列数
    ##获取工号
    # 获取第二列内容,数据格式为此数据的原有格式(原:字符串,读取:字符串;原:浮点数, 读取:浮点数)
    #下标从0开始为1
    cols_gonghaos = sheet.col_values(1)
    #获取姓名
    cols_names = sheet.col_values(0)
    ##获取出入
    cols_inouts = sheet.col_values(5)
    ##获取出入时间
    cols_times = sheet.col_values(8)

all_content = []
in_time = []
out_time = []
first_out = ""
first_out_time = 0
first_in = ""
first_in_time = 0
in_x = []
x = 1
i = 1
f = True

while i < rows-1:
    i = x
    x = i
    if x >= rows-1:#判断下标是否越界,越界即结束
        break
    while f == True:
        if x >= rows-1: #判断下标是否越界,越界即结束
            break
        if cols_names[x] == cols_names[x+1]:   #匹配  当前名字  和  后面一个名字  是否一样
            if cols_inouts[x] == cols_inouts[x+1]: #匹配当前名字下 如果都是  入或者出 跳过记录
                x = x + 1
                break
            elif cols_inouts[x] == '入' and cols_inouts[x+1] == '出':  #匹配  如果先是  入 后一个是 出  的话跳过
                x = x + 1
                break
            else:
                #获取出去时间
                first_out_time = cols_times[x] 
                date_tuple_out = xldate_as_tuple(first_out_time, 0)  # 元组类型数据
                date_object_out = datetime(*date_tuple_out)  # 日期时间对象
                #获取进入时间
                first_in_time  = cols_times[x+1]
                date_tuple_in = xldate_as_tuple(first_in_time, 0)
                date_object_in = datetime(*date_tuple_in)

                start = date_object_out
                end = date_object_in

                time = (end - start).total_seconds() #这个time为多少秒
                # time = (end - start).total_seconds()/60  #这个time为多少分钟的计算
                #清空
                first_out = ""
                first_out_time = 0
                first_in = ""
                first_in_time = 0

                if abs(time) > 10 and abs(time) < 120:
                    all_content.append(int(cols_gonghaos[i]))
                    in_time.append(date_object_in)
                    out_time.append(date_object_out)
                    in_x.append(x) #用于确定筛选出来的时间在单元格的第几行
                
                x = x + 2
        else:
            x = x + 1
            break

with open("学号.txt", 'w') as f:
        for i in all_content:
            f.write(str(i) + '\n')

with open("进入时间.txt", 'w') as f:
        for i in in_time:
            f.write(str(i) + '\n')

with open("出去时间.txt", 'w') as f:
        for i in out_time:
            f.write(str(i) + '\n')

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

czb203 发表于 2023-3-21 16:09
感谢分享,学习一下
Curiousliya 发表于 2023-3-21 16:15
dg87171888 发表于 2023-3-21 16:17
liuziEatOnlyOne 发表于 2023-3-21 16:26
感谢楼主分享,学习一下
a2523188267 发表于 2023-3-21 16:43
两个excle  每个excle ???这
狐白本白 发表于 2023-3-21 17:01
使用pandas更方便只需要合并一下excel 然后判断时间列就可以了
 楼主| Lengxiy 发表于 2023-3-21 18:02
a2523188267 发表于 2023-3-21 16:43
两个excle  每个excle ???这

我只能更改上面的文件一个一个文件跑了拉,毕竟还不太会,对我来说最蠢最简单的方法就是更改文件名,跑程序,反正老师给我甩了两个excle,每天两个
 楼主| Lengxiy 发表于 2023-3-21 18:03
狐白本白 发表于 2023-3-21 17:01
使用pandas更方便只需要合并一下excel 然后判断时间列就可以了

谢大哥,有时间研究研究pandas,主要是现学现做搜的关键词是:python 如何处理excle就这样了,所以先学习的xlrd
a2523188267 发表于 2023-3-21 18:14
能不能不要有错别字呀
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止回复与主题无关非技术内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-23 18:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表