需要建议如何从几个列表中创建第三个列表。 第一个是这样的(大约20000行): LIST1。
field1 field2 field3 field4 field5 field6 field7
---------------------------------------------------------------------------
1167 28669 001f.ce5d.cb4d Gi0/0/1.10 1 Vi2.156 PTA
848 32350 c83a.350d.f368 Gi0/0/1.10 1 Vi2.601 PTA
1771 43465 c46e.1f7a.4763 Gi0/0/1.10 1 Vi2.959 PTA
1390 24116 dc9f.db01.c6e8 Gi0/0/1.10 1 Vi2.32 PTA
712 23579 d850.e6d5.cb1c Gi0/0/1.10 1 Vi2.436 PTA
1239 28354 2828.5dd4.bc65 Gi0/0/1.10 1 Vi2.78 PTA
204 27816 e03f.491d.9978 Gi0/0/1.10 1 Vi2.341 PTA
383 28368 60e3.278c.7199 Gi0/0/1.10 1 Vi2.114 PTA
671 54657 c46e.1f81.a3d3 Gi0/0/1.10 1 Vi2.224 PTA
第二个是这样的(约20000行): 列表2
field1 field2 field3 field4 field5
---------------------------------------------------------------------
Vi2.1 0001799 PPPoE 00:00:08 10.100.146.30
Vi2.2 0010129 PPPoE 00:00:08 10.100.148.108
Vi2.4 0010173 PPPoE 00:00:08 10.100.150.56
Vi2.5 0011093 PPPoE 00:00:08 10.100.146.143
Vi2.6 0003301 PPPoE 00:43:48 10.100.150.107
Vi2.7 0010101 PPPoE 00:00:08 10.100.147.133
Vi2.8 0001859 PPPoE 00:00:08 10.100.145.223
Vi2.9 0010049 PPPoE 06:45:08 10.100.147.138
Vi2.10 0003515 PPPoE 00:00:28 10.100.146.173
Vi2.11 0001747 PPPoE 00:00:18 10.100.146.37
Vi2.12 0011060 PPPoE 04:40:28 10.100.149.165
Vi2.13 0001335 PPPoE 00:00:08 10.239.152.165
Vi2.14 0010154 PPPoE 00:00:08 10.100.148.68
我需要创建第三个列表,并且需要这样的顺序:
field6(list1) Field2(list1) field3(list1) field2(list2) field5(list2)
顺便说一下。 list1中的Field6与列表2中的field1相同。
我明白,我需要从list1中获取每一行,使其成为一个字段列表,然后取出字段6并转到list2并在list2中搜索该值。
然后将所有需要的字段收集到一个新行中。任何人,我解析非常非常新,请举几个例子来说明如何处理这个(我认为是典型的)任务!
澄清。 我通过python 3 telnetlib接收了这些行,如下所示:
import telnetlib
HOST = '2.22.22.22'
password = "user"
user = "user"
tn = telnetlib.Telnet(HOST)
tn.read_until(b"Username: ")
tn.write(user.encode('ascii') + b"\n")
tn.read_until(b"Password: ")
tn.write(password.encode('ascii') + b"\n")
tn.write(b"term len 0 \n")
tn.write(b"show pppoe session | exclude 7878.7878.7878 \n")
tn.write(b"\n exit\n")
mystring = tn.read_all().decode('ascii').replace('\r\n', '\n')
print(mystring)
temp_list = mystring.splitlines()
print(temp_list)
mylist = ["\n".join(s for s in temp_list if 'PTA' in s and 'Vi2' in s)]
答案 0 :(得分:0)
You might want to hack numpy。 loadtxt
方法从文本文件加载表格数据并将其转换为数组(不是本机python)。
通过这种方便的方法,您可以轻松实现结果。
>>> import numpy as np
>>> t1 = "/home/ziya/Projects/python/test/list1.txt"
>>> t2 = "/home/ziya/Projects/python/test/list1.txt"
>>> d1 = np.loadtxt(t1, skiprows=2, dtype='str')
>>> d2 = np.loadtxt(t2, skiprows=2, dtype='str')
>>> d1_field1 = [i[0] for i in d1]
>>> d2_field1 = [i[1] for i in d1]
>>> d3_field1 = [i[2] for i in d1]
>>> d4_field2 = [i[3] for i in d2]
>>> d5_field2 = [i[4] for i in d2]
>>> d6_field2 = [i[5] for i in d2]
>>> new_list = []
>>> new_list.append(d1_field1)
>>> new_list.append(d2_field1)
>>> new_list.append(d3_field1)
>>> new_list.append(d4_field2)
>>> new_list.append(d5_field2)
>>> new_list.append(d6_field2)
>>> new_list
[['1167', '848', '1771', '1390', '712', '1239', '204', '383', '671'], ['28669', '32350', '43465', '24116', '23579', '28354', '27816', '28368', '54657'], ['001f.ce5d.cb4d', 'c83a.350d.f368', 'c46e.1f7a.4763', 'dc9f.db01.c6e8', 'd850.e6d5.cb1c', '2828.5dd4.bc65', 'e03f.491d.9978', '60e3.278c.7199', 'c46e.1f81.a3d3'], ['Gi0/0/1.10', 'Gi0/0/1.10', 'Gi0/0/1.10', 'Gi0/0/1.10', 'Gi0/0/1.10', 'Gi0/0/1.10', 'Gi0/0/1.10', 'Gi0/0/1.10', 'Gi0/0/1.10'], ['1', '1', '1', '1', '1', '1', '1', '1', '1'], ['Vi2.156', 'Vi2.601', 'Vi2.959', 'Vi2.32', 'Vi2.436', 'Vi2.78', 'Vi2.341', 'Vi2.114', 'Vi2.224']]
>>> np.array(new_list).transpose()
array([['1167', '28669', '001f.ce5d.cb4d', 'Gi0/0/1.10', '1', 'Vi2.156'],
['848', '32350', 'c83a.350d.f368', 'Gi0/0/1.10', '1', 'Vi2.601'],
['1771', '43465', 'c46e.1f7a.4763', 'Gi0/0/1.10', '1', 'Vi2.959'],
['1390', '24116', 'dc9f.db01.c6e8', 'Gi0/0/1.10', '1', 'Vi2.32'],
['712', '23579', 'd850.e6d5.cb1c', 'Gi0/0/1.10', '1', 'Vi2.436'],
['1239', '28354', '2828.5dd4.bc65', 'Gi0/0/1.10', '1', 'Vi2.78'],
['204', '27816', 'e03f.491d.9978', 'Gi0/0/1.10', '1', 'Vi2.341'],
['383', '28368', '60e3.278c.7199', 'Gi0/0/1.10', '1', 'Vi2.114'],
['671', '54657', 'c46e.1f81.a3d3', 'Gi0/0/1.10', '1', 'Vi2.224']],
dtype='|S14')