回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>如果行的字段2是“IT”,我有代码搜索要匹配的第13列,如果行的字段2是“FW”,搜索要匹配的第14列。(根据“l”中的数据)。。这将分成几行,每行都以“TEC”开头</p>
<pre><code>"IT"[13thCOL] start numbers to be matched from FILTER variables.
"FW"[14thCOL] start numbers to be matched from FILTER variables.
</code></pre>
<p>我试着把逻辑放在IF..ELSE条件下,但不知为什么我的IF条件不起作用。你知道吗</p>
<pre><code>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))
</code></pre>
<p>获取输出为</p>
<pre><code>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
</code></pre>
<p>所需输出:</p>
<pre><code>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
</code></pre>
<p>从IF..ELSE条件,我想把所有匹配的行打印成一个变量。我不知道为什么与“IT”匹配的行会打印两次。。你知道吗</p>