如何使用 Python 动态创建列表字典
How to create a list dict dynamically with Python
我有这个输入要处理:
['', '', '', '', ' Huawei Integrated Access Software (MA5600T).', ' Copyright(C) Huawei Technologies Co., Ltd. 2002-2014. All rights reserved.', '', ' -----------------------------------------------------------------------------', ' User last login information:', ' -----------------------------------------------------------------------------', ' 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', ' F/S/P : 0/17/7', ' Ont SN : 48575443A9517A9E (HWTC-A9517A9E)', ' Password : 0x00000000000000000000', ' Loid : ', ' Checkcode : ', ' VendorID : HWTC', ' Ont Version : 159D.A', ' Ont SoftwareVersion : V5R019C00S100', ' Ont EquipmentID : EG8145V5', "---- 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)
但是returns这个,我的目标就快达到了:
[{}, {'F/S/P': '0/17/7'}, {'F/S/P': '0/17/7', 'Ont SN': '485754437D85CA9E '}, {'F/S/P': '0/17/7', 'Ont SN': '485754437D85CA9E ', 'Password': None}, {'F/S/P': '0/17/7', 'Ont SN': '48575443A9517A9E ', 'Password': None}]
但我想删除无用的对象,如 {}, {'F/S/P': '0/17/7'}
有人可以帮我解决这个问题吗?或者可以给我一个提示吗?
提前致谢
我不确定您对此 autofind_list
的计划是什么,但我认为这是您尝试解决 records
中没有出现一条记录的问题。这可以通过在循环完成后简单地添加 current_record
来解决。
那么唯一的问题就是第一条空记录,因为开头有一个不必要的空行。这可以通过在将 current_record
添加到 records
之前简单地检查是否已填充来完成(如果连续两个空行,这也可以防止出现问题)。
完整代码如下:
records = []
current_record = {}
for line in data_return:
line = line.strip()
if not line: # empty line
if current_record: # no data recorded
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
if "Ont SN" in line:
key, value = line.split(':')
key = key.strip()
value = value.strip()
value = re.sub(r'\(.*\)', '', value)
current_record[key] = value
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
if current_record:
records.append(current_record)
records
现在包含我认为您想要的列表。
由于 post 中的示例数据实际上并不代表您给出的输入,其中记录由 ----
虚线分隔,因此您需要替换 if not line:
和 if not line or set(line) == {'-'}:
.
我有这个输入要处理:
['', '', '', '', ' Huawei Integrated Access Software (MA5600T).', ' Copyright(C) Huawei Technologies Co., Ltd. 2002-2014. All rights reserved.', '', ' -----------------------------------------------------------------------------', ' User last login information:', ' -----------------------------------------------------------------------------', ' 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', ' F/S/P : 0/17/7', ' Ont SN : 48575443A9517A9E (HWTC-A9517A9E)', ' Password : 0x00000000000000000000', ' Loid : ', ' Checkcode : ', ' VendorID : HWTC', ' Ont Version : 159D.A', ' Ont SoftwareVersion : V5R019C00S100', ' Ont EquipmentID : EG8145V5', "---- 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)
但是returns这个,我的目标就快达到了:
[{}, {'F/S/P': '0/17/7'}, {'F/S/P': '0/17/7', 'Ont SN': '485754437D85CA9E '}, {'F/S/P': '0/17/7', 'Ont SN': '485754437D85CA9E ', 'Password': None}, {'F/S/P': '0/17/7', 'Ont SN': '48575443A9517A9E ', 'Password': None}]
但我想删除无用的对象,如 {}, {'F/S/P': '0/17/7'}
有人可以帮我解决这个问题吗?或者可以给我一个提示吗?
提前致谢
我不确定您对此 autofind_list
的计划是什么,但我认为这是您尝试解决 records
中没有出现一条记录的问题。这可以通过在循环完成后简单地添加 current_record
来解决。
那么唯一的问题就是第一条空记录,因为开头有一个不必要的空行。这可以通过在将 current_record
添加到 records
之前简单地检查是否已填充来完成(如果连续两个空行,这也可以防止出现问题)。
完整代码如下:
records = []
current_record = {}
for line in data_return:
line = line.strip()
if not line: # empty line
if current_record: # no data recorded
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
if "Ont SN" in line:
key, value = line.split(':')
key = key.strip()
value = value.strip()
value = re.sub(r'\(.*\)', '', value)
current_record[key] = value
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
if current_record:
records.append(current_record)
records
现在包含我认为您想要的列表。
由于 post 中的示例数据实际上并不代表您给出的输入,其中记录由 ----
虚线分隔,因此您需要替换 if not line:
和 if not line or set(line) == {'-'}:
.