“在openpyxl中的/:'NoneType'和'float'”不受支持的操作数types
我正在尝试读取Excel文件中的列,并将数据写入Python(使用Openpyxl)中列的相应文本文件中。 Excel文件中缺less一些数据,如下面的第二列:
“gappy.xlsx”:
350.2856445313 -424.5273132324 -322.6161499023 609.8883056641 -453.6102294922 780.6325683594 -628.981628418
这是我的代码:
import openpyxl wb = openpyxl.load_workbook('gappy.xlsx', data_only = True) ws = wb.active factor = 1.0 for i in range (1,4): with open('text'+str(i)+'.txt', "wt") as file_id: for j in range (1,ws.max_row+1): file_id.write("{:.3e}\n".format(ws.cell(row = j,column = i).value/factor)) # I've also tried this, but I cannot write a file separately for each cloumn ... # for column in ws.columns: # for cell in column: # print(cell.value) # file_id.write('%f\n' % (ws.columns[i][0].value))
然后,我得到这个错误:
TypeError:不支持的操作数types为/:'NoneType'和'float'
原因在于第二列的空格被认为是“无”。 我知道读第二列到'max_row + 1'是错误的,但我不知道还有什么要做。 请告诉我如何解决这个问题。 先谢谢你。
那么你可以在做分割之前检查两个参数都不是None。 如果一个人是None
处理它以你想要的任何方式。
像这样的东西
if ws.cell(row = j,column = i).value is not None and factor is not None: file_id.write("{:.3e}\n".format(ws.cell(row = j,column = i).value/factor)) else: # handle it in a different fashion