问题描述
我要处理这个输入:
['','',' Huawei Integrated Access Software (MA5600T).',' copyright(C) Huawei Technologies Co.,Ltd. 2002-2014. All rights reserved.',' -----------------------------------------------------------------------------',' User last login @R_977_4045@ion:',' Access Type : Telnet ',' IP-Address : 1.1.1.1',' Login Time : 2020-12-24 11:51:33+01:00',' logout Time : 2020-12-24 11:51:38+01:00','OLT-SALUZZO_01>enable','OLT-SALUZZO_01#display ont autofind all \x1b[1D\x1b[1C',' ----------------------------------------------------------------------------',' Number : 1',' F/S/P : 0/17/7',' Ont SN : 485754437D85CA9E (HWTC-7D85CA9E)',' Password : 0x00000000000000000000',' Loid : ',' Checkcode : ',' vendorID : HWTC',' Ont Version : 159D.A',' Ont Softwareversion : V5R019C00S100',' Ont EquipmentID : EG8145V5',' Ont autofind time : 2020-12-24 08:38:28+01:00',' Number : 2',' Ont SN : 48575443A9517A9E (HWTC-A9517A9E)',"---- More ( Press 'Q' to break ) ----"]
我需要从中创建这样的输出:
[{'F/S/P': '0/17/7','SN': ' 485754437D85CA9E','Password': None},{'F/S/P': '0/17/7','SN': '48575443A9517A9E ','Password': None}]
我的功能是这样的:
tn.write("display ont autofind all ".encode('utf-8') + b"\n")
return_lineid = tn.read_until('The number of GPON'.encode('utf-8'),3).decode('utf-8')
data_return = return_lineid.splitlines()
autofind_list=[]
records = []
current_record = {}
for line in data_return:
line = line.strip()
if not line: # empty line
records.append(current_record)
current_record = {}
else:
if "F/S/P" in line:
key,value = line.split(':')
key = key.strip()
value = value.strip()
current_record[key] = value
print(current_record)
if "Ont SN" in line:
key,value = line.split(':')
key = key.strip()
value = value.strip()
value = re.sub(r'\(.*\)',value)
current_record[key] = value
print(current_record)
if "Password" in line:
key,value = line.split(':')
key = key.strip()
value = value.strip()
if value == '0x00000000000000000000':
current_record[key]=None
else:
current_record[key] = value
autofind_list.append(current_record.copy())
#for removing same records
seen = set()
new_l = []
for d in autofind_list:
t = tuple(d.items())
if t not in seen:
seen.add(t)
new_l.append(d)
print(new_l)
但它返回这个,我几乎达到了我的目标:
[{},{'F/S/P': '0/17/7'},'Ont SN': '485754437D85CA9E '},'Ont SN': '485754437D85CA9E ','Ont SN': '48575443A9517A9E ','Password': None}]
但我想删除无用的对象,如 {},{'F/S/P': '0/17/7'}
提前致谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)