Python:If..else。。印刷线路的状况

Python:If..else。。印刷线路的状况,python,python-3.x,Python,Python 3.x,如果行的字段2是“IT”,我有代码搜索第13列要匹配,如果行的字段2是“FW”,我有代码搜索第14列要匹配。(根据“l”中的数据。这将分成几行,每行以“TEC”开头) 我试图在IF..ELSE条件中加入逻辑,但不知何故,我的IF条件工作得不好 import operator from functools import reduce l = ['TEC,FW,AS.example.111,311496,20180509042152033,20180509042152033,0,480,j8vg

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

我试图在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”匹配的行会打印两次

更新

由于您更改了原始问题和代码(您不应该这样做),这里有一个新问题的修复程序

改变

result.append([i代表l中的i,如果有的话(x代表i.split(','))[12]代表过滤器中的x)])

如果有(对于过滤器中的x,r[12]中的x):
结果.追加(行)
原始答案

看起来,
te
是一个字符串,表示循环时

te中的行的

在没有索引1(第二个元素)的每个字符上循环(即,
是单个字符)

我认为要完成你想要的,你应该删除这条线

te='\n'.加入(l)
它将列表合并为单个字符串。而是用这个

te=[x.split(',')表示l中的x]
它使用列表理解将
l
中的每一行拆分为一个列表,您可以使用
行[1]

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))
更新

输出

更新:说明

当第2列为
IT
时,您对第13列感兴趣。因此,
r[12]
给出了如下内容:
sip:+44149878722
。 当第二列为FW时,您对第十四列感兴趣。因此,
r[13]
给出了类似于:
sip:+442086081330@example.com

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

所以要知道是
r[12]
sip:+44149878722
)还是
r[13]
sip:+442086081330@example.com
)元素位于
过滤器中,
您必须获取前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:+44149878722
。因此,前8个字符是
r[12][:8]
,即
sip:+441
。前7名 字符为
r[12][:7]
,即
sip:+44
。前10个字符是
r[12][:10]
,即
sip:+44149
。现在,你可以 请参见,对于此
r[12]
sip:+441498722
),只有
r[12][:10]
sip:+44149
)在
过滤器
变量中。所以,你可以 将此
添加到
结果
列表中


这是相同的逻辑,当
行的第二列
FW

时,我的答案没有逻辑(代码在
if..elif…
bloc中)。当字段为“It”和“FW”时,最好为
结果提供所需的结果。您好,谢谢您的帮助。在按照下面所做的更改之后,我现在可以在没有索引器的情况下执行,但如果..ELSE条件出现问题,我就不会应用append。我的要求是在同一个“output”变量中查看所有行,所以请您帮助我如何使用append to“output”变量。最好是“upvote”一个代码来帮助您。更新您的帖子,并为我们提供所需输出的示例。检查您的示例
l
。对于这个例子,我认为,匹配的不是5行而是3行……在
所需的输出中:
,前三行是重复的。因此,通常情况下,
所需输出应包含三行而不是五行。。。检查一下!!你好,科德雷菲,我已经更新了我的问题,并给出了所需的输出。。我的逻辑在个别情况下运行良好,但在IF..ELSE条件下,我不确定为什么它会多次打印行。。如果(过滤器中的r[12][:8]或(过滤器中的r[12][:7]或(过滤器中的r[12][:10]),您可以简化过滤器检查如果语句以保持原始语法,请帮助我理解这一行。请参阅我的答案以获取示例@marain您的第一个代码,当您执行
result.append([i代表l中的i,如果有(x代表i.split(','))[13]代表x代表FILTER)]
时,您可以在
结果
列表中添加另一个列表。之后,你可能很难实现你的目标……现在,如果你不理解这种语法的原因:
r[12][:8]在FILTER中,或者r[12][:7]在FILTER中,或者r[12][:10]在FILTER
,我可以解释一下。
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
if (r[12][:8] in FILTER) or (r[12][:7] in FILTER) or (r[12][:10] in FILTER):
    # code