Python:如果..否则。。印刷线状况

2024-05-13 08:38:55 发布

您现在位置:Python中文网/ 问答频道 /正文

如果行的字段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”匹配的行会打印两次。。你知道吗


Tags: comexampleasitmobilesiptecfw
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))

Update

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

Output

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

UPDATE: Explanations

当第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中, 如果其中一个(至少)在FILTER中,则必须获取前8、7和10个字符并测试。你知道吗

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。你知道吗

更新

既然你改变了原来的问题和代码(你不应该这么做),这里有一个新问题的修正

改变

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]对其进行索引

相关问题 更多 >