Python 如果其中一个单元格为空,如何删除行
以下是电子表格:Python 如果其中一个单元格为空,如何删除行,python,replace,Python,Replace,以下是电子表格: Color Name Size red Apple large green Apple small orange Orange small pea Green super small 在这里,我用Apple_对象替换Apple的所有实例,并删除任何非Apple的名称: for x in name: if 'Apple' not in name: name = name.replace(x, ''
Color Name Size
red Apple large
green Apple small
orange Orange small
pea Green super small
在这里,我用Apple_对象替换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')
填写(姓名):
如何删除所有没有名称的行
所需输出:
Color Name Size
red Apple_Object large
green Apple_Object small
谢谢 试试这个。它过滤掉不包含空字符串的行,剩下的行则保留
result=filter(lambda x:''不在x中,列表)
您正在白白替换apple的值
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]
您使用什么数据类型存储整个表?这不是列表。您正在读取某种csv文件吗?
list
没有replace
方法,只有字符串有。在迭代过程中修改列表也会带来麻烦不要将list
用作变量名,因为这会影响内置的list
类型。这会使你的代码读起来很混乱,并可能导致神秘的bug。你有点缺少一个。这会很有帮助。如果要从中删除,则需要向后遍历它。如果看到“编辑”,则它实际上不会存储在列表中。
for x in list:
if 'Apple' not in list:
del list[index]