如果其中一个单元格为空,如何删除行

时间:2016-07-22 15:57:20

标签: python replace

这是电子表格:

Color  Name      Size
red    Apple     large
green  Apple     small
orange Orange    small
pea    Green     super small

在这里,我用Apple_Object替换Apple的所有实例,并删除任何不是apple的名称:

for x in name:
    if 'Apple' not in name:
         name = name.replace(x, '')


for x in name:
    name = name.replace('Apple', 'Apple_Object')
  

sheet.write(名称):

Color    Name             Size
red      Apple_Object     large
green    Apple_Object     small
orange                    small
pea                       super small

如何删除没有名称的所有行?

期望的输出:

 Color    Name             Size
  red      Apple_Object     large
  green    Apple_Object     small

谢谢!

2 个答案:

答案 0 :(得分:1)

你正在替换苹果的价值

for x in list:
  if 'Apple' not in list:
     list = list.replace(x, '')

但你应该删除当前行

for x in list:
  if 'Apple' not in list:
     del list[index]

答案 1 :(得分:0)

试一试。它过滤掉包含空字符串的行,剩下的就是

result = filter(lambda x: '' not in x, list)