Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x if语句中的字符串匹配无效_Python 3.x - Fatal编程技术网

Python 3.x if语句中的字符串匹配无效

Python 3.x if语句中的字符串匹配无效,python-3.x,Python 3.x,我对Python非常陌生,但我不太清楚我的代码到底出了什么问题 我有一个csv文件,并且,我正在尝试根据所述行的内容删除行。我使用csv模块创建了一个列表列表,列表中的每个列表都是一行 第一部分如我所预期的那样工作。它遍历所有数据行,如果字符串匹配为true,则忽略该行并在代码中打印语句。如果为false,它将获取该行并将其附加到名为“inactive_list”的新列表中 print('Inactive Pass\n') inactive_list.insert(0, data[0]) for

我对Python非常陌生,但我不太清楚我的代码到底出了什么问题

我有一个csv文件,并且,我正在尝试根据所述行的内容删除行。我使用csv模块创建了一个列表列表,列表中的每个列表都是一行

第一部分如我所预期的那样工作。它遍历所有数据行,如果字符串匹配为true,则忽略该行并在代码中打印语句。如果为false,它将获取该行并将其附加到名为“inactive_list”的新列表中

print('Inactive Pass\n')
inactive_list.insert(0, data[0])
for row in range(1, len(data)):
    if not 'Active' in data[row][9]:
        print('Removed row #' + str(row) + '\n' + str(data[row]) +'\n')
    else:
        inactive_list.append(data[row])
print(inactive_list)
在相同的函数中,我有第二个for循环,如下所示:

print('\n\n\nDemo Pass')
for row in range(1, len(inactive_list)):
    if 'demo' or 'demo'.upper() or 'demo'.title() in inactive_list[row][0]:
        print('Removed row #' + str(row) + '\n' + str(data[row]) +'\n')
    else:
        demo_list.append(data[row])
print(demo_list)
这个循环与上面的循环非常相似,不同之处在于我试图在列表中每个列表的索引0处匹配“demo”、“demo”或“demo”——如果匹配,则执行与第一个循环相同的操作。本质上是将不匹配的行追加到新列表

第二个循环只匹配每一行,不管每个列表的索引0中有什么

我对编程非常生疏,如果这有点基本,我深表歉意。

请更改以下内容

if 'demo' or 'demo'.upper() or 'demo'.title() in inactive_list[row][0]:


我不太懂python,但我会尝试
if'demo in inactive_-list[row][0]或'demo'。在inactive_-list[row][0]或'demo.title]的上方。title()in inactive_-list[row][0]:
我怀疑
if'demo'
语句的计算结果实际上是
if'demo'!=无
因此,您询问
是否为“演示”!=无或“演示”。上限()None或'demo'.title()(在非活动列表[行][0]中):
任何人都知道这种语言并能确认这一点吗?是的,您的逻辑是正确的@SamMcould还使用内置函数来实现可能更容易理解的语法
(如果有)('demo'或'demo'.upper()或'demo'.title()(在非活动列表[行][0]):
,但萨姆的逻辑表明了这一点fix@SamM已经一针见血了,现在它起作用了。谢谢
if inactive_list[row][0] in ('demo', 'demo'.upper(), 'demo'.title()):