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') 
    
  • 相关阅读:
    Win7安装netbeans 找不到JDK
    MyEclipse10 中设置Jquery提醒,亲测可用
    Property 'submit' of object #<HTMLFormElement> is not a function
    JSP Unable to compile class for JSP
    JSP session过期时间(小记)
    JSP乱码(小记)
    JS 正则表达式基础
    CSS 盒子模型
    Cmder--window开发者神器!!
    encodeURI和encodeURIComponent两者的区别
  • 原文地址:https://www.cnblogs.com/retry/p/11737141.html
Copyright © 2011-2022 走看看