简体   繁体   English

如何避免使用python从Excel读取#Div / 0

[英]How to avoid reading #Div/0 from Excel using python

I'm using openpyxl to read some numerical values from Excel files, while proceeding to read the numbers on a column I want to avoid the division by zero cells. 我正在使用openpyxl从Excel文件中读取一些数值,同时继续读取一列上的数字,我想避免被零单元格除。 I know that there are 4 or 5 among 100 numbers. 我知道100个数字中有4个或5个。 I used the if not conditions in the way: 我用了if if条件:

N=[]
If not ZerodivisionError:
    N.append(cell.value)

Else
    Break

But this turns the list empty. 但这使列表为空。 If I don't use the condition the error numbers will also be present in the list 如果我不使用该条件,则错误编号也会出现在列表中

I found the answer to my question and would like to leave it here to be more useful : 我找到了我问题的答案,并希望将其留在此处更加有用:

Since division by zero is as '#DIV/0!', that's enough to set the condition : 由于被零除为'#DIV / 0!',这就足以设置条件:

if  type(cell.value) is not str :

and the cell will be avoided 并且将避免使用该单元格

Why not trap the error in the excel spreadsheet earlier: 为什么不更早地在Excel电子表格中捕获错误:

=IFERROR(your_formula,0)

Replace the zero as suits your application : a blank or text etc. 替换零以适合您的应用程序:空白或文本等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM