如何在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
how to check if a cell is empty in openpyxl python
提问by Newboy11
I'm making a conditional statement in openpyxl
Python to check if a cell is empty. Here's my code:
我正在用openpyxl
Python做一个条件语句来检查单元格是否为空。这是我的代码:
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 prevsymbol
with str
"None", not None
object. Try
您prevsymbol
与str
“无”进行比较,而不是None
反对。尝试
if prevsymbol == None:
Also here
也在这里
prevsymbol = readex.cell(row = looprow,column=getnewhighcolumn).value
you use looprow
as row index. And you increment looprow
only if cell.value
is not empty. Here
您looprow
用作行索引。并且looprow
只有在cell.value
不为空时才增加。这里
newstocks.append(prevsymbol)
you use newstocks
instead 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 等效项。