import xlrd
def get_sensor_dict(filePath):
excelfile = xlrd.open_workbook(filePath)
sheets = excelfile.sheets()
contentDict = {}
rowName = ""
for sheet in sheets:
rows = sheet.nrows
rowDict = None
sheetDict = {}
tagList = sheet.row_values(1)
for r in range(2,rows):
rowData = sheet.row_values(r)
if rowData[0]:
if not rowDict:
rowDict = {}
else:
sheetDict[rowName] = rowDict
rowDict = {}
rowName = rowData[0]
block = {}
for i in range(2,len(rowData)):
block[tagList[i]] = rowData[i]
rowDict[rowData[1]] = block
sheetDict[rowName] = rowDict
contentDict[sheet.name] = sheetDict
return contentDict
def get_device_state(filePath):
excelfile = xlrd.open_workbook(filePath)
sheets = excelfile.sheets()
contentDict = {}
rowName = ""
rowName1 = ""
for sheet in sheets:
rows = sheet.nrows
rowDict = None
rowDict1 = None
for r in range(3,rows):
rowData = sheet.row_values(r)
if rowData[0]:
if not rowDict:
rowDict = {}
else:
contentDict[rowName] = rowDict
rowDict = {}
rowName = rowData[0]
if rowData[1]:
if not rowDict1:
rowDict1 = {}
else:
rowDict[rowName1] = rowDict1
rowDict1 = {}
rowName1 = rowData[1]
rowDict1[rowData[2]] = rowData[3]
contentDict[rowName] = rowDict
return contentDict
def get_pro_info(filePath):
excelfile = xlrd.open_workbook(filePath)
sheets = excelfile.sheets()
contentDict = {}
for sheet in sheets:
rows = sheet.nrows
tagList = sheet.row_values(1)
for r in range(2,rows):
rowData = sheet.row_values(r)
print(rowData)
def show_dict(inDict,indent = 0):
for key in inDict.keys():
if type(inDict[key]) == type({}):
for i in range(indent):
if i%4 == 0:
print('|',end='')
else:
print('-',end='')
print('|',key,":")
show_dict(inDict[key],indent+4)
else:
for i in range(indent):
if i%4 == 0:
print('|',end='')
else:
print('-',end='')
print('|',key," : ",inDict[key])
if __name__ == "__main__":
content = get_sensor_dict("a.xls")
show_dict(content)
content = get_device_state("b.xls")
show_dict(content)
这个执行出来只有a里面的数据,而提示b不支持匹配
具体的错误提示可以提供一下吗?需要根据错误提示排查代码问题。
Traceback (most recent call last):
File "C:\Users\aywayw\Desktop\excel.py", line 98, in <module>
content = get_device_state("设备状态监控.xls")
File "C:\Users\aywayw\Desktop\excel.py", line 58, in get_device_state
rowDict1[rowData[2]] = rowData[3]
TypeError: 'NoneType' object does not support item assignment