python实现xlsx文件分析详解

坚持就是胜利。胜利不重要,重要的是能坚持人生最大的哀痛,是子欲孝而亲不在!人生最大的悲剧,是家未富而人先亡,人生最大的可怜,是弥留之际才明白自己是应该做什么的!

python脚本实现xlsx文件解析,供大家参考,具体内容如下

环境配置:

1.系统环境:Windows 7 64bit
2.编译环境:Python3.4.3
3.依赖库: os sys xlrd re
4.其他工具:none
5.前置条件:待处理的xlsx文件

脚本由来

最近的工作是做测试,而有一项任务呢,就是分析每天机器人巡检时采集的数据,包括各种传感器,CO2、O2、噪声等等,每天的数据也有上千条,通过站控的导出数据功能,会把数据库里面导出成xlsx文件,而这项任务要分析一下当天采集的数据是否在正常范围,要计算摄像头的识别率和识别准确率,自己傻呵呵的每天都在手动操作,突然觉得很浪费时间,索性写个python脚本吧,这样每天一条命令,就能得到自己想看的数据结果。每天至少节省10分钟!
这是要解析的xlsx文件:


一般手动就得筛选、排序、打开计算器计算 - - 繁琐枯燥乏味
还是python大法好

代码浅析

流程图

脚本demo

#-*- coding:utf-8 -*-
import xlrd
import os
import sys
import logging
import re
#logging.basicConfig(level=logging.DEBUG)

xfile = sys.argv[1]

dateList = []
InspectionType = []
InspectionRresult = []

def load_data():

  CO2Type = []
  O2Type = []
  NoiseType = []
  SupwareType = []
  TowareType = []
  TemperatureType = []
  HumidityType = []
  InfraredType = []

  CO2Result = []
  O2Result = []
  NoiseResult = []
  SupwareResult = []
  TowareResult = []
  TemperatureResult = []
  HumidityResult = []
  InfraredResult = []

  logging.debug(InspectionType)
  logging.debug(InspectionRresult)


  for index, value in enumerate(InspectionType):
    if value == "二氧化碳":                   #CO2Type
      CO2Type.extend(value)
      logging.debug(index)
      logging.debug("CO2 RESULT:  "+InspectionRresult[index])
      CO2Result.append(InspectionRresult[index])

    if value == "氧气传感器":                  #O2Type
      O2Type.extend(value)
      O2Result.append(InspectionRresult[index])

    if value == "噪声传感器":                  #NoiseType
      NoiseType.extend(value)
      NoiseResult.append(InspectionRresult[index])


    if value == "局放(超声波测量)":               #SupwareType
      SupwareType.extend(value)
      SupwareResult.append(InspectionRresult[index])

    if value == "局放(地电波测量)":               #SupwareType
      TowareType.extend(value)
      TowareResult.append(InspectionRresult[index])

    if value == "温度传感器":                  #TemperatureType
      TemperatureType.extend(value)
      TemperatureResult.append(InspectionRresult[index])      

    if value == "湿度传感器":                  #TemperatureType
      HumidityType.extend(value)
      HumidityResult.append(InspectionRresult[index])

    if value == "温度(红外测量)":                  #TemperatureType
      InfraredType.extend(value)
      InfraredResult.append(InspectionRresult[index])      
  logging.debug(CO2Result)
  logging.debug(O2Result)
  logging.debug(NoiseResult)
  logging.debug(SupwareResult)
  logging.debug(TowareResult)
  logging.debug(TemperatureResult)
  logging.debug(HumidityResult)    
  logging.debug(InfraredResult)   
  return CO2Result,O2Result,NoiseResult,SupwareResult,TowareResult,TemperatureResult,HumidityResult,InfraredResult

def get_data_print(co2,o2,noise,supware,toware,temperature,humidity,infrared):
  co2 = list(map(eval,co2))
  o2 = list(map(eval,o2))
  noise = list(map(eval,noise))
  supware = l