zoukankan      html  css  js  c++  java
  • python分析二进制文件写入excel

    import os
    import sys
    import struct
    import binascii
    import xlwt
    
    def ParseData(parsebytes):
        i=0
        j=1
        data_len=len(parsebytes)
        while(i<data_len-1):
            try:
                send_flag=struct.unpack('b',parsebytes[i+2:i+3])[0]
                pint_id=struct.unpack('<H',parsebytes[i+3:i+5])[0]  
                dev_id=struct.unpack('<H',parsebytes[i+5:i+7])[0]  
                order=struct.unpack('<I',parsebytes[i+7:i+11])[0]  
                cur_time=struct.unpack('<I',parsebytes[i+11:i+15])[0]
                cur_data=struct.unpack('<I',parsebytes[i+39:i+43])[0]
                #写入excel表格进行后续分析
                sheet.write(j, 0, send_flag)
                sheet.write(j, 1, pint_id)
                sheet.write(j, 2, dev_id)
                sheet.write(j, 3, order)
                sheet.write(j, 4, cur_time)
                sheet.write(j, 5, cur_data)
            except:
                print('memory is overflow')        
            finally:
                i=i+47
                j=j+1
    
    
     
    #切换到指定目录
    os.chdir(r'C:UsersUsmartDesktopfile_test')
    
    #创建excel表格
    book=xlwt.Workbook(encoding="utf-8",style_compression=0)
    sheet = book.add_sheet('表1', cell_overwrite_ok=True) #cell_overwrite_ok,表示是否可以覆盖单元格
    sheet.write(0, 0, '发送标志')
    sheet.write(0, 1, '采集点ID')
    sheet.write(0, 2, '设备ID')
    sheet.write(0, 3, '流水号')
    sheet.write(0, 4, '采集时间')
    sheet.write(0, 5, '设备数据')
    
    #读取历史数据的二进制文件进行解析
    fp=open("history.dat",'rb') 
    filedata = fp.read()
    ParseData(filedata)
    print('Auto test finish')
    fp.close()
    
    #保存并生成excel文件
    book.save('历史统计.xls') 
    
  • 相关阅读:
    July 08th. 2018, Week 28th. Sunday
    July 07th. 2018, Week 27th. Saturday
    兄弟组件bus传值
    vue 父子组件传值
    路由传值的三种方式
    jQuery 操作表格
    原生js实现开关功能
    跨域解决方法
    正则判断密码难度
    cookie封装函数
  • 原文地址:https://www.cnblogs.com/retry/p/11737141.html
Copyright © 2011-2022 走看看