建行手機(jī)app下載安裝seo的基本步驟包括哪些
目錄
?前言
正文
1.Python基礎(chǔ)學(xué)習(xí)
2.Python讀取Excel表格
2.1安裝xlrd模塊
2.2使用介紹
2.2.1常用單元格中的數(shù)據(jù)類型
2.2.2?導(dǎo)入模塊
2.2.3打開Excel文件讀取數(shù)據(jù)
2.2.4常用函數(shù)
2.2.5代碼測試
2.2.6 Python操作Excel官方網(wǎng)址
3.Python創(chuàng)建xml文件
3.1 xml語法學(xué)習(xí)
3.2創(chuàng)建xml文件
3.3?代碼測試
4.解決問題
5.總結(jié)
?前言
ECU診斷工作,需要實(shí)現(xiàn)的DTC(Diagnostic trouble code)接近500個(gè),每個(gè)DTC的配置項(xiàng)基本類似。也就是說需要在AUTOSAR工具上重復(fù)手動(dòng)配置500個(gè)DTC。AUTOSAR工具還非常的卡。
分析整個(gè)操作過程為:Excel表格讀取一個(gè)DTC碼-->ISOLAR工具中手動(dòng)配置一個(gè)DTC-->重復(fù)500次?-->?最后生成配置文件Dem_Config.axml.
Axml文件中每一項(xiàng)DTC的配置內(nèi)容如下,基本就是修改DTC碼。
基于以上重復(fù)且有規(guī)律的工作需求,考慮使用Python腳本搞定。
正文
1.Python基礎(chǔ)學(xué)習(xí)
推薦菜鳥教程:https://www.runoob.com/python3/python3-install.html
整個(gè)學(xué)習(xí)過程推薦兩到3天完成。
2.Python讀取Excel表格
python操作excel主要用到xlrd和xlwt這兩個(gè)庫,即xlrd是讀excel,xlwt是寫excel的庫。這里只用到xlrd模塊。
2.1安裝xlrd模塊
命令行:pip install xlrd
2.2使用介紹
2.2.1常用單元格中的數(shù)據(jù)類型
0: empty,?
1: string(text),?
2:number, 3:date,?
4: boolean,?
5: error,?
b: blank(空白)
2.2.2?導(dǎo)入模塊
Import xlrd
2.2.3打開Excel文件讀取數(shù)據(jù)
Data = xlrd.open_workbook(r’filename’)
2.2.4常用函數(shù)
Excel當(dāng)中最重要的就是對(duì)book(工作簿)和sheet(工作表)的操作
1)獲取book中一個(gè)工作表
table = data.sheets()[0] #通過索引順序獲取
table = data.sheet_by_index(sheet_indx)) #通過索引順序獲取
table = data.sheet_by_name(sheet_name) #通過名稱獲取
以上三個(gè)函數(shù)都會(huì)返回一個(gè)xlrd.sheet.Sheet()對(duì)象
names = data.sheet_names() #返回book中所有工作表的名字
data.sheet_loaded(sheet_name or indx) # 檢查某個(gè)sheet是否導(dǎo)入完畢
2)行的操作
nrows = table.nrows #獲取該sheet中的有效行數(shù)
table.row(rowx) #返回由該行中所有的單元格對(duì)象組成的列表
table.row_slice(rowx) #返回由該列中所有的單元格對(duì)象組成的列表
table.row_types(rowx, start_colx=0, end_colx=None) #返回由該行中所有單元格的數(shù)據(jù)類型組成的列表
table.row_values(rowx, start_colx=0, end_colx=None) #返回由該行中所有單元格的數(shù)據(jù)組成的列表
table.row_len(rowx) #返回該列的有效單元格長度
3)列的操作
ncols = table.ncols #獲取列表的有效列數(shù)
table.col(colx, start_rowx=0, end_rowx=None) #返回由該列中所有的單元格對(duì)象組成的列表
table.col_slice(colx, start_rowx=0, end_rowx=None) #返回由該列中所有的單元格對(duì)象組成的列表
table.col_types(colx, start_rowx=0, end_rowx=None) #返回由該列中所有單元格的數(shù)據(jù)類型組成的列表
table.col_values(colx, start_rowx=0, end_rowx=None) #返回由該列中所有單元格的數(shù)據(jù)組成的列表
4)單元格的操作
table.cell(rowx,colx) #返回單元格對(duì)象
table.cell_type(rowx,colx) #返回單元格中的數(shù)據(jù)類型
table.cell_value(rowx,colx) #返回單元格中的數(shù)據(jù)
2.2.5代碼測試
測試Excel表格
測試代碼:
#!/usr/bin/env python
import xlrd
?
data = xlrd.open_workbook(r'.\book.xlsx')
?
def xlrd_read():table = data.sheets()[0] #通過索引順序獲取第一個(gè)sheet1
#行操作print("There are %s rows in sheet1" %(table.nrows)) #獲取該sheet中的有效行數(shù)print("Return type of table.row(): ", type(table.row(0))) #返回由該行中所有的單元格對(duì)象組成的列表print("Items of row 1 on shee1: ", table.row(1)) #第2行列表元素print("Items of row 2 on sheet1: ", table.row_slice(2)) #返回由該行中所有的單元格對(duì)象組成的列表print(table.row_types(1, start_colx=0, end_colx=None)) #返回由該行中所有單元格的數(shù)據(jù)類型組成的列表print(table.row_values(1, start_colx=0, end_colx=None)) #返回由該行中所有單元格的數(shù)據(jù)組成的列表print("Length of row 2: ", table.row_len(1)) #返回該行的有效單元格長度#列操作print("There are %s columns in sheet1" %(table.ncols)) #獲取列表的有效列數(shù)print(table.col(0, start_rowx=0, end_rowx=None)) #返回由該列中所有的單元格對(duì)象組成的列表print(table.col_slice(0, start_rowx=0, end_rowx=None)) # 返回由該列中所有的單元格對(duì)象組成的列表print(table.col_types(0, start_rowx=0, end_rowx=None)) # 返回由該列中所有單元格的數(shù)據(jù)類型組成的列表print(table.col_values(0, start_rowx=0, end_rowx=None)) # 返回由該列中所有單元格的數(shù)據(jù)組成的列表#單元格操作print(table.cell(1, 1)) # 返回單元格對(duì)象print(table.cell_type(1, 1)) # 返回單元格中的數(shù)據(jù)類型print(table.cell_value(1, 1)) # 返回單元格中的數(shù)據(jù)
def main():xlrd_read()
if?__name__?==?'__main__':main()
測試結(jié)果
2.2.6 Python操作Excel官方網(wǎng)址
http://www.python-excel.org/
3.Python創(chuàng)建xml文件
3.1 xml語法學(xué)習(xí)
https://www.runoob.com/xml/xml-tutorial.html
3.2創(chuàng)建xml文件
ElementTree是python的XML處理模塊,它提供了一個(gè)輕量級(jí)的對(duì)象模型。它在Python2.5以后成為Python標(biāo)準(zhǔn)庫的一部分,但是Python2.4之前需要單獨(dú)安裝。在使用ElementTree模塊時(shí),需要import xml.etree.ElementTree的操作。
ElementTree表示整個(gè)XML節(jié)點(diǎn)樹,而Element表示節(jié)點(diǎn)數(shù)中的一個(gè)單獨(dú)的節(jié)點(diǎn)。
構(gòu)建XML文件
ElementTree(tag),其中tag表示根節(jié)點(diǎn),初始化一個(gè)ElementTree對(duì)象。
Element(tag, attrib={}, **extra)函數(shù)用來構(gòu)造XML的一個(gè)根節(jié)點(diǎn),其中tag表示根節(jié)點(diǎn)的名稱,attrib是一個(gè)可選項(xiàng),表示節(jié)點(diǎn)的屬性。
SubElement(parent, tag, attrib={}, **extra)用來構(gòu)造一個(gè)已經(jīng)存在的節(jié)點(diǎn)的子節(jié)點(diǎn)。
Element.text和SubElement.text表示element對(duì)象的額外的內(nèi)容屬性,Element.tag和Element.attrib分別表示element對(duì)象的標(biāo)簽和屬性。
ElementTree.write(file, encoding='us-ascii', xml_declaration=None, default_namespace=None, method='xml'),函數(shù)新建一個(gè)XML文件,并且將節(jié)點(diǎn)數(shù)數(shù)據(jù)寫入XML文件中。
3.3?代碼測試
import xml.etree.ElementTree as ET
?
def buildNewsXmlFile():# 設(shè)置一個(gè)新節(jié)點(diǎn),并設(shè)置其標(biāo)簽為rootroot = ET.Element("root")# 在root下新建兩個(gè)子節(jié)點(diǎn),設(shè)置其名稱分別為sina和chinabytesina = ET.SubElement(root, "sina")chinabyte = ET.SubElement(root, "chinabyte")# 在sina下新建兩個(gè)子節(jié)點(diǎn),設(shè)置其節(jié)點(diǎn)名稱分別為number和firstsina_number = ET.SubElement(sina, "number")sina_number.text = "1"sina_first = ET.SubElement(sina, "first")sina_first.text = "http://roll.tech.sina.com.cn/internet_all/index_1.shtml"# 在chinabyte下新建兩個(gè)子節(jié)點(diǎn),設(shè)置其節(jié)點(diǎn)名稱為number和firstchinabyte_number = ET.SubElement(chinabyte, "number")chinabyte_number.text = "1"chinabyte_first = ET.SubElement(chinabyte, "first")chinabyte_first.text = "http://www.chinabyte.com/more/124566.shtml"# 將節(jié)點(diǎn)數(shù)信息保存在ElementTree中,并且保存為XML格式文件RawText = ET.tostring(root)dom = minidom.parseString(RawText)f = open(r'.\Test_4.axml', 'w', encoding='utf-8')dom.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')f.close()def main():# xml_read()# xml_write()#xml_create()buildNewsXmlFile()if __name__ == '__main__':main()
4.解決問題
讀取Excel表格提取需要的數(shù)據(jù)?-->?緩存數(shù)據(jù)?-->?遍歷沒有給數(shù)據(jù)生成xml文件?-->?合并到配置文件當(dāng)值?-->?完成所有配置
#!/usr/bin/env python
import xlrd
import xml.etree.ElementTree as ET
?
from xml.dom import minidom
?
ExcelPath?=?r'.\FileName.xlsx'
First_Dtc_RowNum = 25
Last_Dtc_RowNum = 456
DtcCodeHexStr = []
DtcCodeDemStr = []
?
def OpenExcel():global DtcCodeHexStrglobal DtcCodeDemStrdata = xlrd.open_workbook(ExcelPath)table = data.sheet_by_name('DTC')DtcCodeHexStr = table.col_values(2, start_rowx=First_Dtc_RowNum, end_rowx=Last_Dtc_RowNum)for HexStr in DtcCodeHexStr:int_10 = int(str(HexStr), 16)int_10_str = str(int_10)DtcCodeDemStr.append(int_10_str)print("Type of DtcCodeHexStr = ",type(DtcCodeHexStr))print("DtcCodeHexStr = ",DtcCodeHexStr)print("Type of DtcCodeDemStr = ", type(DtcCodeDemStr))print("DtcCodeDemStr = ", DtcCodeDemStr)
?
def CreatDemDtcXml():global First_Dtc_RowNumglobal Last_Dtc_RowNumglobal DtcCodeHexStrglobal DtcCodeDemStrCONTAINERS = ET.Element("CONTAINERS")for i in range(Last_Dtc_RowNum - First_Dtc_RowNum):EcuContainrValue = ET.SubElement(CONTAINERS, "ECUC-CONTAINER-VALUE")ET.SubElement(EcuContainrValue, "SHORT-NAME").text = "DemDTC_" + DtcCodeHexStr[i]ET.SubElement(EcuContainrValue, "DEFINITION-REF", {"DEST": "ECUC-PARAM-CONF-CONTAINER-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTC"ParameterValues = ET.SubElement(EcuContainrValue, "PARAMETER-VALUES")EcucTextualParamValue = ET.SubElement(ParameterValues, "ECUC-TEXTUAL-PARAM-VALUE")ET.SubElement(EcucTextualParamValue, "DEFINITION-REF",{"DEST":"ECUC-ENUMERATION-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTC/DemDTCSeverity"ET.SubElement(EcucTextualParamValue, "VALUE").text = "DEM_SEVERITY_NO_SEVERITY"EcuNumerivalParamValue = ET.SubElement(ParameterValues, "ECUC-NUMERICAL-PARAM-VALUE")ET.SubElement(EcuNumerivalParamValue, "DEFINITION-REF", {"DEST":"ECUC-INTEGER-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTC/DemDtcValue"ET.SubElement(EcuNumerivalParamValue, "VALUE").text = DtcCodeDemStr[i]ReferenceValues = ET.SubElement(EcuContainrValue, "REFERENCE-VALUES")EcucReferenceValue = ET.SubElement(ReferenceValues, "ECUC-REFERENCE-VALUE")ET.SubElement(EcucReferenceValue, "DEFINITION-REF",{"DEST":"ECUC-REFERENCE-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemDTC/DemDTCAttributesRef"ET.SubElement(EcucReferenceValue, "VALUE-REF",{"DEST":"ECUC-CONTAINER-VALUE"}).text = "/ETAS_Project/EcucModuleConfigurationValuess/Dem/DemConfigSet_0/DemDTCAttributes"RawText?=?ET.tostring(CONTAINERS)dom = minidom.parseString(RawText)f = open(r'.\DemDtc.axml', 'w', encoding='utf-8')dom.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')f.close()def CreatDemEventParameterXml():global First_Dtc_RowNumglobal Last_Dtc_RowNumglobal DtcCodeHexStrglobal DtcCodeDemStrCONTAINERS = ET.Element("CONTAINERS")for i in range(Last_Dtc_RowNum - First_Dtc_RowNum):EcuContainrValue = ET.SubElement(CONTAINERS, "ECUC-CONTAINER-VALUE")ET.SubElement(EcuContainrValue, "SHORT-NAME").text = "DemEventParameter_" + DtcCodeHexStr[i]ET.SubElement(EcuContainrValue,"DEFINITION-REF").text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter"ParameterValues = ET.SubElement(EcuContainrValue, "PARAMETER-VALUES")EcucNumericalParamValue_0 = ET.SubElement(ParameterValues, "ECUC-NUMERICAL-PARAM-VALUE")ET.SubElement(EcucNumericalParamValue_0, "DEFINITION-REF", {"DEST": "ECUC-INTEGER-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemEventFailureCycleCounterThreshold"ET.SubElement(EcucNumericalParamValue_0, "VALUE").text = "1"EcucTextualParamValue_0 = ET.SubElement(ParameterValues, "ECUC-TEXTUAL-PARAM-VALUE")ET.SubElement(EcucTextualParamValue_0, "DEFINITION-REF", {"DEST": "ECUC-ENUMERATION-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemReportBehavior"ET.SubElement(EcucTextualParamValue_0, "VALUE").text = "REPORT_BEFORE_INIT"EcucNumericalParamValue_1 = ET.SubElement(ParameterValues, "ECUC-NUMERICAL-PARAM-VALUE")ET.SubElement(EcucNumericalParamValue_1, "DEFINITION-REF", {"DEST": "ECUC-BOOLEAN-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemEventAvailable"ET.SubElement(EcucNumericalParamValue_1, "VALUE").text = "true"EcucTextualParamValue_1 = ET.SubElement(ParameterValues, "ECUC-TEXTUAL-PARAM-VALUE")ET.SubElement(EcucTextualParamValue_1, "DEFINITION-REF", {"DEST": "ECUC-ENUMERATION-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemEventKind"ET.SubElement(EcucTextualParamValue_1, "VALUE").text = "DEM_EVENT_KIND_BSW"EcucNumericalParamValue_2 = ET.SubElement(ParameterValues, "ECUC-NUMERICAL-PARAM-VALUE")ET.SubElement(EcucNumericalParamValue_2, "DEFINITION-REF", {"DEST": "ECUC-BOOLEAN-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemFFPrestorageSupported"ET.SubElement(EcucNumericalParamValue_2, "VALUE").text = "false"ReferenceValues = ET.SubElement(EcuContainrValue, "REFERENCE-VALUES")EcucReferenceValue_0 = ET.SubElement(ReferenceValues, "ECUC-REFERENCE-VALUE")ET.SubElement(EcucReferenceValue_0, "DEFINITION-REF", {"DEST": "ECUC-REFERENCE-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemDTCRef"ET.SubElement(EcucReferenceValue_0, "VALUE-REF", {"DEST": "ECUC-CONTAINER-VALUE"}).text = "/ETAS_Project/EcucModuleConfigurationValuess/Dem/DemConfigSet_0/DemDTC_" + DtcCodeHexStr[i]EcucReferenceValue_1 = ET.SubElement(ReferenceValues, "ECUC-REFERENCE-VALUE")ET.SubElement(EcucReferenceValue_1, "DEFINITION-REF", {"DEST": "ECUC-REFERENCE-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemOperationCycleRef"ET.SubElement(EcucReferenceValue_1, "VALUE-REF", {"DEST": "ECUC-CONTAINER-VALUE"}).text = "/ETAS_Project/EcucModuleConfigurationValuess/Dem/DemGeneral/DemOperationCycle_Other"SubContainers = ET.SubElement(EcuContainrValue, "SUB-CONTAINERS")EcucContainerValu_0 = ET.SubElement(SubContainers, "ECUC-CONTAINER-VALUE")ET.SubElement(EcucContainerValu_0, "SHORT-NAME").text = "DemRbEventClass_0"ET.SubElement(EcucContainerValu_0, "DEFINITION-REF", {"DEST": "ECUC-PARAM-CONF-CONTAINER-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemRbEventClass"ParameterValues_0 = ET.SubElement(EcucContainerValu_0, "PARAMETER-VALUES")EcucNumericalParamValue_3 = ET.SubElement(ParameterValues_0, "ECUC-NUMERICAL-PARAM-VALUE")ET.SubElement(EcucNumericalParamValue_3, "DEFINITION-REF", {"DEST": "ECUC-INTEGER-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemRbEventClass/DemRbEventBufferTime"ET.SubElement(EcucNumericalParamValue_3, "VALUE").text = "0"EcucNumericalParamValue_4 = ET.SubElement(ParameterValues_0, "ECUC-NUMERICAL-PARAM-VALUE")ET.SubElement(EcucNumericalParamValue_4, "DEFINITION-REF", {"DEST": "ECUC-BOOLEAN-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemRbEventClass/DemRbEventRecoverableInSameOperationCycle"ET.SubElement(EcucNumericalParamValue_4, "VALUE").text = "1"EcucNumericalParamValue_5 = ET.SubElement(ParameterValues_0, "ECUC-NUMERICAL-PARAM-VALUE")ET.SubElement(EcucNumericalParamValue_5, "DEFINITION-REF", {"DEST": "ECUC-BOOLEAN-PARAM-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemRbEventClass/DemRbEventStatusBitStorageTestFailed"ET.SubElement(EcucNumericalParamValue_5, "VALUE").text = "0"EcucContainerValu_1 = ET.SubElement(SubContainers, "ECUC-CONTAINER-VALUE")ET.SubElement(EcucContainerValu_1, "SHORT-NAME").text = "DemDebounceAlgorithmClass"ET.SubElement(EcucContainerValu_1, "DEFINITION-REF", {"DEST": "ECUC-CHOICE-CONTAINER-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemDebounceAlgorithmClass"SubContainers_1 = ET.SubElement(EcucContainerValu_1, "SUB-CONTAINERS")EcucContainerValu_2 = ET.SubElement(SubContainers_1, "ECUC-CONTAINER-VALUE")ET.SubElement(EcucContainerValu_2, "SHORT-NAME").text = "DemDebounceCounterBased"ET.SubElement(EcucContainerValu_2, "DEFINITION-REF", {"DEST": "ECUC-PARAM-CONF-CONTAINER-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemDebounceAlgorithmClass/DemDebounceCounterBased"ReferenceValues_0 = ET.SubElement(EcucContainerValu_2, "REFERENCE-VALUES")EcucReferenceValue_2 = ET.SubElement(ReferenceValues_0, "ECUC-REFERENCE-VALUE")ET.SubElement(EcucReferenceValue_2, "DEFINITION-REF", {"DEST": "ECUC-REFERENCE-DEF"}).text = "/AUTOSAR_Dem/EcucModuleDefs/Dem/DemConfigSet/DemEventParameter/DemDebounceAlgorithmClass/DemDebounceCounterBased/DemDebounceCounterBasedClassRef"ET.SubElement(EcucReferenceValue_2, "VALUE-REF", {"DEST": "ECUC-CONTAINER-VALUE"}).text = "/ETAS_Project/EcucModuleConfigurationValuess/Dem/DemConfigSet_0/DemDebCntClass_U127_D127"RawText?=?ET.tostring(CONTAINERS)dom = minidom.parseString(RawText)f = open(r'.\DemEventParameter.axml', 'w', encoding='utf-8')dom.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')f.close()
?
def main():OpenExcel()CreatDemDtcXml()CreatDemEventParameterXml()
?
if __name__ == '__main__':main()
導(dǎo)入Python生成的xml文件到Davinci/ISOALR等AUTOSAR工具,即可完成所有DTC的配置。
5.總結(jié)
每個(gè)人在處理Excel數(shù)據(jù)的時(shí)候,如果發(fā)現(xiàn)是重復(fù)且有規(guī)律的工作,都可以考慮使用Python來提高效率。當(dāng)然,Python的功能還有很多很多,把基本知識(shí)學(xué)好,后面遇到什么就學(xué)什么,有目的的去學(xué)要快很多。