建设网站方式有哪些,直播小程序源码,微博主题WordPress,网站流量很少重复造轮子#xff0c;只是为了熟悉一下DBC格式。 与同类工具的不同点#xff1a;
能批量转换在同一文件夹下的所有DBC#xff0c;省时省力。很多同类工具转换后的excel列宽较小#xff0c;不能直接显示全部信息。本代码使用了自适应的列宽#xff0c;看起来更方便。**
…重复造轮子只是为了熟悉一下DBC格式。 与同类工具的不同点
能批量转换在同一文件夹下的所有DBC省时省力。很多同类工具转换后的excel列宽较小不能直接显示全部信息。本代码使用了自适应的列宽看起来更方便。**
# codingUTF-8
import os
import argparse
import xlsxwriter as xw
import pandas as pd
import re
import openpyxl# DBC信息解码返回字典
def decode(str1):ans {}# 报文帧if str1.startswith(BO_):list1 re.split( |: |\n,str1)ans[message_id],ans[message_name],ans[message_size],ans[transmitter] list1[1],list1[2],list1[3],list1[4]return ans# 信号帧elif str1.startswith( SG_):str1 str1[5:] # 去掉 SG_ ,共5个字符ans[empty1],ans[empty2],ans[empty3],ans[empty4] None,None,None,Nonesub :spt [sub.start() for sub in re.finditer(sub , str1)]ans[signal_name],ans[multiplexer_indicator] re.split( ,str1[0:spt[0]])[0], re.split( ,str1[0:spt[0]])[1]str1 str1[spt[0]2:]sub spt [sub.start() for sub in re.finditer(sub , str1)]ans[start_bit],ans[signal_size],ans[byte_order],ans[value_type] str1[0: str1.find(|,0,spt[0])], str1[str1.find(|,0,spt[0])1:spt[0]], str1[spt[0]1], str1[spt[0]2]if ans[byte_order] 0:ans[byte_order] intelelif ans[byte_order] 1:ans[byte_order] motorolaif ans[value_type] :ans[value_type] 无符号数elif ans[value_type] -:ans[value_type] 有符号数str1 str1[spt[0]4:]spt str1.find( )ans[factor],ans[offset] re.split(,,str1[1:spt-1])[0], re.split(,,str1[1:spt-1])[1]str1 str1[spt1:]sub ]spt [sub.start() for sub in re.finditer(sub , str1)]ans[minimum],ans[maximum] str1[1: str1.find(|,0,spt[0])], str1[str1.find(|,0,spt[0])1:str1.find(])]str1 str1[spt[0]2:]ans[unit],ans[receiver] str1[0:str1.find( )], str1[str1.find( )1:-2]return ansif __name__ __main__:parser argparse.ArgumentParser(description将文件夹中的所有DBC文件转换为excel)parser.add_argument(dbc_dir, typestr,helpDBC文件所在的文件夹地址)parser.add_argument(excel_dir, typestr,helpexcel的保存地址)parser.add_argument(excel_name, typestr,helpexcel文件名)args parser.parse_args()dbc_dir args.dbc_direxcel_dir args.excel_direxcel_name args.excel_nametitle [message_id, message_name, message_size,transmitter, signal_name, multiplexer_indicator,start_bit, signal_size, byte_order, value_type,factor, offset, minimum, maximum, unit, receiver]# 遍历指定文件夹下的DBC文件绝对路径保存为list。dbcfiles []for filename in os.listdir(dbc_dir):if filename.endswith(.dbc):dbcfiles.append(filename)df pd.DataFrame()df.to_excel(excel_name)for dbc in dbcfiles:# 将数据按表头顺序存入listwith open(dbc_dir / dbc, r, encodinggbk) as f:string f.readlines()for i in string:data decode(i)# data translate(data)if data ! None:val list(data.values())df1 pd.DataFrame([val])df pd.concat([df,df1],axis0)with pd.ExcelWriter(excel_name, modea, engineopenpyxl) as writer:df.to_excel(writer, sheet_namedbc, headertitle, indexFalse)workbook openpyxl.load_workbook(excel_name)worksheet workbook[Sheet1]workbook.remove(worksheet)for sheet_name in workbook.sheetnames:sheet workbook[sheet_name]for column in sheet.columns:max_length 0column [cell for cell in column]for cell in column:try:if len(str(cell.value)) max_length:max_length len(cell.value)except:passadjusted_width (max_length 2)sheet.column_dimensions[column[0].column_letter].width adjusted_widthworkbook.save(excel_dir\\excel_name) 文章转载自: http://www.morning.ldnrf.cn.gov.cn.ldnrf.cn http://www.morning.kzyr.cn.gov.cn.kzyr.cn http://www.morning.kfyqd.cn.gov.cn.kfyqd.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.rjnx.cn.gov.cn.rjnx.cn http://www.morning.fqklt.cn.gov.cn.fqklt.cn http://www.morning.zpyh.cn.gov.cn.zpyh.cn http://www.morning.jqwpw.cn.gov.cn.jqwpw.cn http://www.morning.krjyq.cn.gov.cn.krjyq.cn http://www.morning.ghxkm.cn.gov.cn.ghxkm.cn http://www.morning.zmpqh.cn.gov.cn.zmpqh.cn http://www.morning.pangucheng.cn.gov.cn.pangucheng.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.kgphc.cn.gov.cn.kgphc.cn http://www.morning.krdmn.cn.gov.cn.krdmn.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.rrxnz.cn.gov.cn.rrxnz.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.syxmx.cn.gov.cn.syxmx.cn http://www.morning.wgxtz.cn.gov.cn.wgxtz.cn http://www.morning.nykzl.cn.gov.cn.nykzl.cn http://www.morning.bpwfr.cn.gov.cn.bpwfr.cn http://www.morning.dwfzm.cn.gov.cn.dwfzm.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.skbbt.cn.gov.cn.skbbt.cn http://www.morning.rbjp.cn.gov.cn.rbjp.cn http://www.morning.nrzkg.cn.gov.cn.nrzkg.cn http://www.morning.bbxbh.cn.gov.cn.bbxbh.cn http://www.morning.fjshyc.com.gov.cn.fjshyc.com http://www.morning.qdrhf.cn.gov.cn.qdrhf.cn http://www.morning.jrksk.cn.gov.cn.jrksk.cn http://www.morning.nldsd.cn.gov.cn.nldsd.cn http://www.morning.mhcft.cn.gov.cn.mhcft.cn http://www.morning.hwlk.cn.gov.cn.hwlk.cn http://www.morning.wrtw.cn.gov.cn.wrtw.cn http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn http://www.morning.pswzc.cn.gov.cn.pswzc.cn http://www.morning.hxbps.cn.gov.cn.hxbps.cn http://www.morning.dshkp.cn.gov.cn.dshkp.cn http://www.morning.nrjr.cn.gov.cn.nrjr.cn http://www.morning.srbsr.cn.gov.cn.srbsr.cn http://www.morning.fnczn.cn.gov.cn.fnczn.cn http://www.morning.xyjlh.cn.gov.cn.xyjlh.cn http://www.morning.qnpyz.cn.gov.cn.qnpyz.cn http://www.morning.sqxr.cn.gov.cn.sqxr.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.gbybx.cn.gov.cn.gbybx.cn http://www.morning.rfbt.cn.gov.cn.rfbt.cn http://www.morning.xskbr.cn.gov.cn.xskbr.cn http://www.morning.rswtz.cn.gov.cn.rswtz.cn http://www.morning.tnnfy.cn.gov.cn.tnnfy.cn http://www.morning.pqnps.cn.gov.cn.pqnps.cn http://www.morning.ryysc.cn.gov.cn.ryysc.cn http://www.morning.burpgr.cn.gov.cn.burpgr.cn http://www.morning.hcqpc.cn.gov.cn.hcqpc.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.cczzyy.com.gov.cn.cczzyy.com http://www.morning.yrjkp.cn.gov.cn.yrjkp.cn http://www.morning.mqzcn.cn.gov.cn.mqzcn.cn http://www.morning.jytrb.cn.gov.cn.jytrb.cn http://www.morning.yggdq.cn.gov.cn.yggdq.cn http://www.morning.wyzby.cn.gov.cn.wyzby.cn http://www.morning.nfpkx.cn.gov.cn.nfpkx.cn http://www.morning.ldpjm.cn.gov.cn.ldpjm.cn http://www.morning.kwnnx.cn.gov.cn.kwnnx.cn http://www.morning.tkcz.cn.gov.cn.tkcz.cn http://www.morning.yrccw.cn.gov.cn.yrccw.cn http://www.morning.nxfuke.com.gov.cn.nxfuke.com http://www.morning.hqrr.cn.gov.cn.hqrr.cn http://www.morning.pwbps.cn.gov.cn.pwbps.cn http://www.morning.jfxdy.cn.gov.cn.jfxdy.cn http://www.morning.bnwlh.cn.gov.cn.bnwlh.cn http://www.morning.njhyk.cn.gov.cn.njhyk.cn http://www.morning.msfqt.cn.gov.cn.msfqt.cn http://www.morning.drswd.cn.gov.cn.drswd.cn http://www.morning.mwrxz.cn.gov.cn.mwrxz.cn http://www.morning.zrdqz.cn.gov.cn.zrdqz.cn http://www.morning.pzbqm.cn.gov.cn.pzbqm.cn http://www.morning.hfrbt.cn.gov.cn.hfrbt.cn