当前位置:   article > 正文

python excel字符串是否为空,如何在openpyxl python中检查单元格是否为空

python中openpyxl单元格无数据怎么表示

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

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.

解决方案

You compare prevsymbol with str "None", not None object. Try

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

newstocks.append(prevsymbol)

you use newstocks instead of newlist. Try this code

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)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/698196
推荐阅读
相关标签
  

闽ICP备14008679号