【问题标题】:Python: If..else.. condition on printing linesPython:If..else.. 打印行的条件
【发布时间】:2019-10-16 13:56:27
【问题描述】:

如果行的字段 2 是“IT”,我有代码来搜索第 13 列要匹配,如果行的字段 2 是“FW”,则要匹配的第 14 列。 (从“l”中的数据..这将分成几行,每行都以“TEC”开头)

"IT"[13thCOL] start numbers to be matched from FILTER variables.
"FW"[14thCOL] start numbers to be matched from FILTER variables.

我试图将逻辑置于 IF..ELSE 条件下,但不知何故我的 if 条件无法正常工作。

import operator
from functools import reduce

l =  ['TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@10.105.86.9,Cisco-CUCM10.5,sip:+PLT8777447585984834,sip:+440093779379739,sip:+42086081330@example.com,sip:+8777447585984834@10.105.86.51:5060,sip:+441344903000@10.110.70.132:5060,sip:+441499737979@example.com,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,CUST,M79_PL01,mobile,447585984834000,+447585984834,onnet,1001501,1001501', 'TEC,FW,AS.example.111,311497,20180509042152355,20180509042152355,0,480,afc81a7e0aefe660dabb2963acf280,7ee0dd6bb34472945b18c959049f514f@10.105.86.9,NotPresent,sip:+PLT8777447585984834,sip:+440093779379739,sip:+442086081330@example.com,sip:+8777447585984834@example.com:5060,sip:+441344903000@10.105.86.13:5060,sip:+441499737979@example.com,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,CUST,M79_PL01,mobile,447585984834000,+447585984834,onnet,1001501,1001501', 'TEC,FW,AS.example.111,311498,20180509042152828,20180509042152828,0,480,afc81a7e0aefe660dabb2963acf280,5f29e2b4ef5a19c6a489aa53f01c000c@10.105.86.9,NotPresent,sip:+PLT8777447585984834,sip:+440093779379739,sip:+442086081330@example.com,sip:+8777447585984834@example.com:5060,sip:+441344903000@10.105.86.13:5060,sip:+441499737979@example.com,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,CUST,M79_PL01,mobile,447585984834000,+447585984834,onnet,1001501,1001501', 'TEC,IT,AS.example.111,311499,20180509042153373,20180509042202478,9105,normal,afc81a7e0aefe660dabb2963acf280,bc4213fa64c67cbdc8e80c5a437a7677@10.105.86.9,NotPresent,sip:+444441499737979,sip:+441499737979,sip:+442086081330@172.16.90.1,sip:+444441499737979@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,1001501', 'TEC,IT,AS.example.111,311500,20180509042358780,20180509042414784,16004,normal,8deba7200aefe860dabb2b5049eb70,279e60f3eb07aa44c55b8a4b804667bf@10.105.86.9,NotPresent,sip:+744441498738722,sip:+441498738722,sip:+442086081330@172.16.90.1,sip:+444441498738722@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUSTOMER02,GROUP01 - CLUSTER1,pstn,+441498738722,sip:+441498738722@example.com,1000201']
FILTER = ['sip:+420', 'sip:+34', 'sip:+44149']
result = []
for row in l:
    r = row.split(',')
    if r[1] == 'IT':
       result.append([i for i in l if any(x in i.split(',')[12] for x in FILTER)])
    elif r[1] == 'FW':
       result.append([i for i in l if any(x in i.split(',')[13] for x in FILTER)])
    else:
       pass
print("result is")
next = reduce(operator.concat, result)
print('\n'.join(next))

输出为

TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@10.105.86.9,Cisco-CUCM10.5,sip:+PLT8777447585984834,sip:+440093779379739,sip:+42086081330@example.com,sip:+8777447585984834@10.105.86.51:5060,sip:+441344903000@10.110.70.132:5060,sip:+441499737979@example.com,CUST,C15_GR01,pstn,+441499737979,sip:+441499737979@example.com,CUST,C15_GR01,mobile,447585984834000,+447585984834,onnet,1001501,1001501
TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@10.105.86.9,Cisco-CUCM10.5,sip:+PLT8777447585984834,sip:+440093779379739,sip:+42086081330@example.com,sip:+8777447585984834@10.105.86.51:5060,sip:+441344903000@10.110.70.132:5060,sip:+441499737979@example.com,CUST,C15_GR01,pstn,+441499737979,sip:+441499737979@example.com,CUST,C15_GR01,mobile,447585984834000,+447585984834,onnet,1001501,1001501
TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@10.105.86.9,Cisco-CUCM10.5,sip:+PLT8777447585984834,sip:+440093779379739,sip:+42086081330@example.com,sip:+8777447585984834@10.105.86.51:5060,sip:+441344903000@10.110.70.132:5060,sip:+441499737979@example.com,CUST,C15_GR01,pstn,+441499737979,sip:+441499737979@example.com,CUST,C15_GR01,mobile,447585984834000,+447585984834,onnet,1001501,1001501
TEC,IT,AS.example.111,311499,20180509042153373,20180509042202478,9105,normal,afc81a7e0aefe660dabb2963acf280,bc4213fa64c67cbdc8e80c5a437a7677@10.105.86.9,NotPresent,sip:+444441499737979,sip:+441499737979,sip:+442086081330@172.16.90.1,sip:+444441499737979@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUST,C15_GR01,pstn,+441499737979,sip:+441499737979@example.com,1001501
TEC,IT,AS.example.111,311500,20180509042358780,20180509042414784,16004,normal,8deba7200aefe860dabb2b5049eb70,279e60f3eb07aa44c55b8a4b804667bf@10.105.86.9,NotPresent,sip:+744441498738722,sip:+441498738722,sip:+442086081330@172.16.90.1,sip:+444441498738722@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUSTOMER02,GROUP01 - CLUSTER1,pstn,+441498738722,sip:+441498738722@example.com,1000201
TEC,IT,AS.example.111,311499,20180509042153373,20180509042202478,9105,normal,afc81a7e0aefe660dabb2963acf280,bc4213fa64c67cbdc8e80c5a437a7677@10.105.86.9,NotPresent,sip:+444441499737979,sip:+441499737979,sip:+442086081330@172.16.90.1,sip:+444441499737979@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUST,C15_GR01,pstn,+441499737979,sip:+441499737979@example.com,1001501
TEC,IT,AS.example.111,311500,20180509042358780,20180509042414784,16004,normal,8deba7200aefe860dabb2b5049eb70,279e60f3eb07aa44c55b8a4b804667bf@10.105.86.9,NotPresent,sip:+744441498738722,sip:+441498738722,sip:+442086081330@172.16.90.1,sip:+444441498738722@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUSTOMER02,GROUP01 - CLUSTER1,pstn,+441498738722,sip:+441498738722@example.com,1000201

需要的输出:

TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@10.105.86.9,Cisco-CUCM10.5,sip:+PLT8777447585984834,sip:+440093779379739,sip:+42086081330@example.com,sip:+8777447585984834@10.105.86.51:5060,sip:+441344903000@10.110.70.132:5060,sip:+441499737979@example.com,CUST,C15_GR01,pstn,+441499737979,sip:+441499737979@example.com,CUST,C15_GR01,mobile,447585984834000,+447585984834,onnet,1001501,1001501
TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@10.105.86.9,Cisco-CUCM10.5,sip:+PLT8777447585984834,sip:+440093779379739,sip:+42086081330@example.com,sip:+8777447585984834@10.105.86.51:5060,sip:+441344903000@10.110.70.132:5060,sip:+441499737979@example.com,CUST,C15_GR01,pstn,+441499737979,sip:+441499737979@example.com,CUST,C15_GR01,mobile,447585984834000,+447585984834,onnet,1001501,1001501
TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@10.105.86.9,Cisco-CUCM10.5,sip:+PLT8777447585984834,sip:+440093779379739,sip:+42086081330@example.com,sip:+8777447585984834@10.105.86.51:5060,sip:+441344903000@10.110.70.132:5060,sip:+441499737979@example.com,CUST,C15_GR01,pstn,+441499737979,sip:+441499737979@example.com,CUST,C15_GR01,mobile,447585984834000,+447585984834,onnet,1001501,1001501
TEC,IT,AS.example.111,311499,20180509042153373,20180509042202478,9105,normal,afc81a7e0aefe660dabb2963acf280,bc4213fa64c67cbdc8e80c5a437a7677@10.105.86.9,NotPresent,sip:+444441499737979,sip:+441499737979,sip:+442086081330@172.16.90.1,sip:+444441499737979@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUST,C15_GR01,pstn,+441499737979,sip:+441499737979@example.com,1001501
TEC,IT,AS.example.111,311500,20180509042358780,20180509042414784,16004,normal,8deba7200aefe860dabb2b5049eb70,279e60f3eb07aa44c55b8a4b804667bf@10.105.86.9,NotPresent,sip:+744441498738722,sip:+441498738722,sip:+442086081330@172.16.90.1,sip:+444441498738722@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUSTOMER02,GROUP01 - CLUSTER1,pstn,+441498738722,sip:+441498738722@example.com,1000201

从 IF..ELSE 条件我想将所有匹配的行打印到单个变量中。我不确定为什么与“IT”匹配的行打印了 2 次..

【问题讨论】:

  • 下面,你有我的答案,没有你的逻辑(代码在if .. elif ... bloc.)。当字段为“IT”且为“FW”时,最好为 result 提供所需的结果...
  • 您好,感谢您的帮助。在按照下面进行更改后,我现在可以在没有 indexError 的情况下执行,但是 IF..ELSE 条件有问题,我没有应用附加。我的要求是在同一个“输出”变量中查看所有行,所以请你帮忙我如何使用附加到“输出”变量。
  • 推荐一个对你有帮助的代码是件好事。更新您的帖子,并为我们提供所需输出的示例..
  • 检查您的示例l。对于这个例子,我认为匹配的不是 5 行而是 3 行...
  • 在您的required output: 中,前三行重复。所以,通常情况下,required output 应该包含三行而不是五行...检查一下!!

标签: python python-3.x


【解决方案1】:

更新

由于您更改了原始问题和代码(您不应该这样做),因此这里修复了您的新问题

改变

result.append([i for i in l if any(x in i.split(',')[12] for x in FILTER)])

if any(x in r[12] for x in FILTER):
    result.append(row)

原始答案

看起来te 是一个字符串,这意味着当你循环时

for row in te

您正在遍历没有索引 1(第二个元素)的每个字符(即 row 是单个字符)

我认为要完成你想要的,你应该删除这一行

te = '\n'.join(l)

将列表组合成一个字符串。而是使用这个

te = [x.split(',') for x in l]

它使用列表解析将l 的每一行拆分为一个列表,您可以使用row[1] 对其进行索引

【讨论】:

    【解决方案2】:
    l = ['TEC,FW,AS.blaram.111,311496,20181009042152033,20181009042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@20.116.21.9,Cisco-CUCM20.5,sip:+ACE8777447585984834,sip:+220037585984834,sip:+42086081330@man.blaram.com,sip:+8777447585984834@20.116.21.51:5060,sip:+221344903000@20.120.70.132:5060,sip:+221491668070@man.blaram.com,CUST56,C15_GR01,pstn,+221491668070,sip:+221491668070@man.blaram.com,CUST56,C15_GR01,mobile,447585984834000,+227585984834,onnet,1001501,1001501', 'TEC,FW,AS.blaram.111,311497,20181009042152355,20181009042152355,0,480,afc81a7e0aefe660dabb2963acf280,7ee0dd6bb34472945b18c959049f514f@20.116.21.9,NotPresent,sip:+ACE8777447585984834,sip:+220037585984834,sip:+222086081330@man.blaram.com,sip:+8777447585984834@man.blaram.com:5060,sip:+221344903000@20.116.21.13:5060,sip:+221491668070@man.blaram.com,CUST56,C15_GR01,pstn,+221491668070,sip:+221491668070@man.blaram.com,CUST56,C15_GR01,mobile,447585984834000,+227585984834,onnet,1001501,1001501', 'TEC,FW,AS.blaram.111,311498,20181009042152828,20181009042152828,0,480,afc81a7e0aefe660dabb2963acf280,5f29e2b4ef5a19c6a489aa53f01c000c@20.116.21.9,NotPresent,sip:+ACE8777447585984834,sip:+220037585984834,sip:+222086081330@man.blaram.com,sip:+8777447585984834@man.blaram.com:5060,sip:+221344903000@20.116.21.13:5060,sip:+221491668070@man.blaram.com,CUST56,C15_GR01,pstn,+221491668070,sip:+221491668070@man.blaram.com,CUST56,C15_GR01,mobile,447585984834000,+227585984834,onnet,1001501,1001501', 'TEC,IT,AS.blaram.111,311499,20181009042153373,20181009042202478,9105,normal,afc81a7e0aefe660dabb2963acf280,bc4213fa64c67cbdc8e80c5a437a7677@20.116.21.9,NotPresent,sip:+224441491668070,sip:+221491668070,sip:+222086081330@1.1.9.1,sip:+224441491668070@man.blaram.com:5060,sip:+221344903000@20.116.21.17:5080,CUST56,C15_GR01,pstn,+221491668070,sip:+221491668070@man.blaram.com,1001501', 'TEC,IT,AS.blaram.111,311500,20181009042358780,20181009042414784,16004,normal,8deba7200aefe860dabb2b5049eb70,279e60f3eb07aa44c55b8a4b804667bf@20.116.21.9,NotPresent,sip:+744441491603331,sip:+221491603331,sip:+222086081330@1.1.9.1,sip:+224441491603331@man.blaram.com:5060,sip:+221344903000@20.116.21.17:5080,CUSTOMER02,GROUP01 - CLUSTER1,pstn,+221491603331,sip:+221491603331@man.blaram.com,1000201']
    
    # I remove this line: te = '\n'.join(l)
    FILTER = ['sip:+220', 'sip:+494', 'sip:+119']
    
    for row in l:
    
        # for each row, you split by ','
        r = row.split(',') # so, r = ['TEC', 'IT', ...]
    
        # r[1] will always be 'IT' or 'FW' 
        if r[1] == 'IT':
    
            # add your logic here
    
        elif r[1] == 'FW':
            # add your logic here
    
    print('\n'.join(result))
    

    更新

    import operator
    from functools import reduce
    
    l =  ['TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@10.105.86.9,Cisco-CUCM10.5,sip:+PLT8777447585984834,sip:+440093779379739,sip:+42086081330@example.com,sip:+8777447585984834@10.105.86.51:5060,sip:+441344903000@10.110.70.132:5060,sip:+441499737979@example.com,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,CUST,M79_PL01,mobile,447585984834000,+447585984834,onnet,1001501,1001501', 'TEC,FW,AS.example.111,311497,20180509042152355,20180509042152355,0,480,afc81a7e0aefe660dabb2963acf280,7ee0dd6bb34472945b18c959049f514f@10.105.86.9,NotPresent,sip:+PLT8777447585984834,sip:+440093779379739,sip:+442086081330@example.com,sip:+8777447585984834@example.com:5060,sip:+441344903000@10.105.86.13:5060,sip:+441499737979@example.com,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,CUST,M79_PL01,mobile,447585984834000,+447585984834,onnet,1001501,1001501', 'TEC,FW,AS.example.111,311498,20180509042152828,20180509042152828,0,480,afc81a7e0aefe660dabb2963acf280,5f29e2b4ef5a19c6a489aa53f01c000c@10.105.86.9,NotPresent,sip:+PLT8777447585984834,sip:+440093779379739,sip:+442086081330@example.com,sip:+8777447585984834@example.com:5060,sip:+441344903000@10.105.86.13:5060,sip:+441499737979@example.com,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,CUST,M79_PL01,mobile,447585984834000,+447585984834,onnet,1001501,1001501', 'TEC,IT,AS.example.111,311499,20180509042153373,20180509042202478,9105,normal,afc81a7e0aefe660dabb2963acf280,bc4213fa64c67cbdc8e80c5a437a7677@10.105.86.9,NotPresent,sip:+444441499737979,sip:+441499737979,sip:+442086081330@172.16.90.1,sip:+444441499737979@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,1001501', 'TEC,IT,AS.example.111,311500,20180509042358780,20180509042414784,16004,normal,8deba7200aefe860dabb2b5049eb70,279e60f3eb07aa44c55b8a4b804667bf@10.105.86.9,NotPresent,sip:+744441498738722,sip:+441498738722,sip:+442086081330@172.16.90.1,sip:+444441498738722@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUSTOMER02,GROUP01 - CLUSTER1,pstn,+441498738722,sip:+441498738722@example.com,1000201']
    FILTER = ['sip:+420', 'sip:+34', 'sip:+44149']
    result = []
    
    for row in l:
        r = row.split(',')
        if r[1] == 'IT':
            if (r[12][:8] in FILTER) or (r[12][:7] in FILTER) or (r[12][:10] in FILTER):
                result.append(row)
    
        if r[1] == 'FW':
            if (r[13][:8] in FILTER) or (r[13][:7] in FILTER) or (r[13][:10] in FILTER):
                result.append(row)
    
    for row in result:
        print(row)
        print() # jump  line between the output result
    

    输出

    TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg3046nhcs2p47ehci3ng6gpgal9hah9dqi1h9hhfaj100,3507b59a19602f0c96792e180d2469d1@10.105.86.9,Cisco-CUCM10.5,sip:+PLT8777447585984834,sip:+440093779379739,sip:+42086081330@example.com,sip:+8777447585984834@10.105.86.51:5060,sip:+441344903000@10.110.70.132:5060,sip:+441499737979@example.com,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,CUST,M79_PL01,mobile,447585984834000,+447585984834,onnet,1001501,1001501
    
    TEC,IT,AS.example.111,311499,20180509042153373,20180509042202478,9105,normal,afc81a7e0aefe660dabb2963acf280,bc4213fa64c67cbdc8e80c5a437a7677@10.105.86.9,NotPresent,sip:+444441499737979,sip:+441499737979,sip:+442086081330@172.16.90.1,sip:+444441499737979@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUST,M79_PL01,pstn,+441499737979,sip:+441499737979@example.com,1001501
    
    TEC,IT,AS.example.111,311500,20180509042358780,20180509042414784,16004,normal,8deba7200aefe860dabb2b5049eb70,279e60f3eb07aa44c55b8a4b804667bf@10.105.86.9,NotPresent,sip:+744441498738722,sip:+441498738722,sip:+442086081330@172.16.90.1,sip:+444441498738722@example.com:5060,sip:+441344903000@10.105.86.17:5080,CUSTOMER02,GROUP01 - CLUSTER1,pstn,+441498738722,sip:+441498738722@example.com,1000201
    

    更新:解释

    当第 2 列是 IT 时,您对第 13 列感兴趣。所以r[12] 给出类似:sip:+441498738722。 当第 2 列是FW 时,您对第 14 列感兴趣。所以,r[13] 给出类似:sip:+442086081330@example.com

    您的过滤器是:FILTER = ['sip:+420', 'sip:+34', 'sip:+44149']。它包含 3 个项目。第一项的长度是8。 第二项的长度为7,最后一项的长度为10

    所以要知道r[12] (sip:+441498738722) 或r[13] (sip:+442086081330@example.com) 元素是否在FILTER 中, 您必须取前 8、7 和 10 个字符并测试其中一个(至少)是否在 FILTER 中。

    if (r[12][:8] in FILTER) or (r[12][:7] in FILTER) or (r[12][:10] in FILTER):
        # code
    

    例如: r[12]sip:+441498738722。所以前 8 个字符是r[12][:8],即sip:+441。第一个 7 字符是r[12][:7],即sip:+44。前 10 个字符是 r[12][:10],即 sip:+44149。现在你可以 看到对于这个r[12] (sip:+441498738722),只有r[12][:10] (sip:+44149) 在FILTER 变量中。这样你就可以 将此row 添加到result 列表中。

    同样的逻辑,row的第二列是FW

    【讨论】:

    • 你好 codrelphi,我已经更新了我的问题,并给出了所需的输出.. 我的逻辑单独工作正常,但在 IF..ELSE 条件下我不确定它为什么要多次打印行.. 请帮助
    • 如果 (r[12][:8] in FILTER) or (r[12][:7] in FILTER) or (r[12][ :10] 在过滤器中):
    • 您可以简化过滤器检查 if 语句以保持原始语法。例如,请参阅我的答案。 @玛丽亚
    • 在您的第一个代码中,当您执行result.append([i for i in l if any(x in i.split(',')[13] for x in FILTER)]) 时,您会在result 列表中添加另一个列表。之后,您可能很难实现自己的目标......
    • 现在,如果你不明白这个语法的 WHY:r[12][:8] in FILTER or r[12][:7] in FILTER or r[12][:10] in FILTER,我可以解释一下。
    猜你喜欢
    • 2019-04-04
    • 2017-05-08
    • 1970-01-01
    • 2018-08-11
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2022-11-28
    相关资源
    最近更新 更多