from openpyxl import load_workbook
class stop_car:
def __init__(self, excel_name):
# 通过openpyxl得到excel里面的数据
self.wb = load_workbook(excel_name)
self.sh = self.wb.active
# 读取excel的数据
def do_excel(self):
all_data = []
for row in range(2, self.sh.max_row + 1):
# 获取车的车牌和出入时间和停留时间
car_date = {"car_number": self.sh.cell(row=row, column=1).value,
"go_time": self.sh.cell(row=row, column=3).value.strftime("%H:%M:%S"),
"quit_time": self.sh.cell(row=row, column=4).value.strftime("%H:%M:%S"),
"stop_time": self.sh.cell(row=row, column=5).value,
"avg_stop_time": ""}
all_data.append(car_date)
return all_data
# 将excel里面的数据进行筛选,对重复的车牌进行过滤去重
def only_car(self):
only_car_number = []
for i in self.do_excel():
if i["car_number"] not in only_car_number:
only_car_number.extend([i["car_number"]])
return only_car_number
# 通过遍历得到车去了几次停车场,并将停车时间相加处去停车场的次数得到平均时间
def avg_stop_time(self):
avg_time = []
for i in self.only_car():
count_time, car_frequency, avg_time_dir = 0, 0, {}
for j in self.do_excel():
if i == j["car_number"]:
car_frequency += 1
count_time += j["stop_time"]
avg_time_dir[i] = count_time / car_frequency
for x, z in avg_time_dir.items():
for h in self.do_excel():
if x == h["car_number"]:
h["avg_stop_time"] = z
avg_time.append(h)
return avg_time
# input 输入车牌号,将符合车牌号的数据取出来
def search_car(self):
c = input("input car number:")
if str(c) in str(self.avg_stop_time()):
for j in self.avg_stop_time():
if str(c) in j["car_number"]:
print(j)
else:
print("查无此车")
stop_car("停车信息.xlsx").search_car()
停车场的数据处理
最新推荐文章于 2024-01-22 13:49:40 发布