python分析⼆进制⽂件写⼊excel import os
import sys
import struct
import binascii
import xlwt
def ParData(parbytes):
i=0
塌塌米j=1
data_len=len(parbytes)
while(i<data_len-1):
try:
nd_flag=struct.unpack('b',parbytes[i+2:i+3])[0]
pint_id=struct.unpack('<H',parbytes[i+3:i+5])[0]
dev_id=struct.unpack('<H',parbytes[i+5:i+7])[0]
昆明公积金
order=struct.unpack('<I',parbytes[i+7:i+11])[0]
cur_time=struct.unpack('<I',parbytes[i+11:i+15])[0]
cur_data=struct.unpack('<I',parbytes[i+39:i+43])[0]
#写⼊excel表格进⾏后续分析
sheet.write(j, 0, nd_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:\Urs\Usmart\Desktop\file_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()
ParData(filedata)
print('Auto test finish')
fp.clo()
#保存并⽣成excel⽂件
国际商务礼仪book.save('历史统计.xls')