如何在openpyxl python中检查单元格是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31475811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:03:49  来源:igfitidea点击:

how to check if a cell is empty in openpyxl python

pythonopenpyxl

提问by Newboy11

I'm making a conditional statement in openpyxlPython to check if a cell is empty. Here's my code:

我正在用openpyxlPython做一个条件语句来检查单元格是否为空。这是我的代码:

newlist = []
looprow = 1
print ("Highest col",readex.get_highest_column())
getnewhighcolumn = readex.get_highest_column()        
for i in range(0, lengthofdict):
    prevsymbol = readex.cell(row = looprow,column=getnewhighcolumn).value
    if prevsymbol == "None":
        pass
    else:
        newstocks.append(prevsymbol)
        looprow += 1
    #print (prevsymbol)
print(newlist)

I tried if prevsymbol == "":and if prevsymbol == null:to no avail.

我尝试过if prevsymbol == "":if prevsymbol == null:但无济于事。

采纳答案by kvorobiev

You compare prevsymbolwith str"None", not Noneobject. Try

prevsymbolstr“无”进行比较,而不是None反对。尝试

if prevsymbol == None:

Also here

也在这里

prevsymbol = readex.cell(row = looprow,column=getnewhighcolumn).value

you use looprowas row index. And you increment looprowonly if cell.valueis not empty. Here

looprow用作行索引。并且looprow只有在cell.value不为空时才增加。这里

newstocks.append(prevsymbol)

you use newstocksinstead of newlist. Try this code

你使用newstocks而不是newlist. 试试这个代码

newlist = []
print ("Highest col",readex.get_highest_column())
getnewhighcolumn = readex.get_highest_column()        
for i in range(0, lengthofdict):
    prevsymbol = readex.cell(row = i+1,column=getnewhighcolumn).value
    if prevsymbol is not None:
        newlist.append(prevsymbol)
print(newlist)

回答by atlspin

Take the quotes away from the None.

从 None 中取出引号。

if prevsymbol is None:

This is the python equivalent of checking if something is equal to null.

这是检查某项是否等于 null 的 Python 等效项。