[英]Remove empty quote in a nested list using python doesn't work
I make simple script to test how I can remove empty quote from a list. 我编写了简单的脚本来测试如何从列表中删除空引号。 The output actually coming from a device output and i save the output onto csv file.
输出实际上来自设备输出,我将输出保存到csv文件中。
This is my code 这是我的代码
mylist = [['Local Interface', '', 'Parent Interface', '', 'Chassis Id', '',
'', '', '', 'Port info', '', '', '', '', 'System Name'], ['ge-0/0/1', '',
'', '', '', ' -', '', '', '', '', '', '', '', '', ' 00:2d:b3:c9:e2:f0', '
606', '', '', '', '', '', '', '', 'myhost-re01', '', '', '', ''], ['ge-
4/0/7', '', '', '', '', ' -', '', '', '', '', '', '', '', '', '
00:2d:b3:c9:e2:f0', ' 628', '', '', '', '', '', '', '', 'myhost-re01', '',
'', '', ''], ['ge-4/0/6', '', '', '', '', ' -', '', '', '', '', '', '', '',
'', ' 00:2d:b3:c9:e2:f0', ' 629', '', '', '', '', '', '', '',
'myhost-re01', '', '', '', ''], ['ge-0/0/4', '', '', '', '', ' -', '', '',
'', '', '', '', '', '', ' 00:2d:b3:c9:e2:f0', ' 138739712', '', '', '', '',
'PE12XC1010', '', '', '', ''], ['ge-0/0/2', '', '', '', '', ' -', '', '',
'', '', '', '', '', '', ' 00:2d:b3:c9:e2:f0', ' gei_1/5', '', '', '', '',
'', 'PE13XC1011', '', '', '', '', ''], ['ge-3/3/0', '', '', '', '', ' -',
'', '', '', '', '', '', '', '', ' 0c:12:12:c7:c1:f7', ' gei_2/3', '', '',
'', '', '', 'PEUTV01-01XT', '', '', ''], ['ge-3/3/4', '', '', '', '', ' -
', '', '', '', '', '', '', '', '', ' f0:1c:2d:22:37:c0', ' 783', '', '',
'', '', '', '', '', 'myhost-re01', ''], ['{master}']]
print(mylist)
mylist = list(filter(None,mylist))
print(mylist)
The print out before and after filtering is same. 过滤前后的打印输出相同。
Seems easy to resolve but not success so far...hope someone could help me. 似乎很容易解决,但到目前为止还没有成功...希望有人可以帮助我。 Thanks
谢谢
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& UPDATE1: I have json api below that need to be parsed with values. &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& UPDATE1:我有低于需要JSON API与值进行解析。 For example lldp info output to be add onto "lldpinfo" or data[2]
例如,将lldp info输出添加到“ lldpinfo”或data [2]
json1 = {
"channel": "scanner",
"action": "device_scan",
"table": "D2",
"device":[]
}
data = "hostname","ipaddress","lldpinfo"
The overall sample result when parse the value as below 解析值时的整体样本结果如下
{
"channel": "scanner",
"action": "device_scan",
"table": "D2",
"device": [
[
"hostname": "test1",
"ipaddress": "192.1.1.1",
"lldpinfo": [
[
"Local port": "xe-3/0/3.0",
"Port Info": " ae31.0",
"Mac address": " b0:c6:9a:63:80:40",
"Chassis Id": "xe-0/1/3.0",
"Neighbour Host Name": "host.xsrt1.net"
],
[
"Local port": "xe-3/0/3.0",
"Port Info": " ae31.0",
"Mac address": " b0:c6:9a:63:80:40",
"Chassis Id": "xe-0/1/3.0",
"Neighbour Host Name": "host.xsrt1.net"
]
]
},
{
"hostnname": "test2",
"ipaddress": "192.1.1.2",
"lldpinfo": [
{
}
]
}
]
}
From above result the lldp info details is parse in [] .I think it should be in {} like below... 从上面的结果来看,lldp信息的详细信息在[]中进行解析。我认为它应该在{}中,如下所示...
"lldpinfo": [
{
"Local port": "xe-3/0/3.0",
"Port Info": " ae31.0",
"Mac address": " b0:c6:9a:63:80:40",
"Chassis Id": "xe-0/1/3.0",
"Neighbour Host Name": "host.xsrt1.net"
},
{
"Local port": "xe-3/0/3.0",
"Port Info": " ae31.0",
"Mac address": " b0:c6:9a:63:80:40",
"Chassis Id": "xe-0/1/3.0",
"Neighbour Host Name": "host.xsrt1.net"
}
]
You can use a nested list comprehension to exclude falsey items (in this case empty strings) from your nested list. 您可以使用嵌套列表推导从嵌套列表中排除虚假项目(在这种情况下为空字符串)。
mylist = [[x for x in line if x] for line in mylist]
See list comprehensions 查看清单理解
This is one list comprehension: 这是一个列表理解:
[x for x in line if x]
nested inside another: 嵌套在另一个内部:
[... for line in mylist]
giving you a nested list as output. 给您一个嵌套列表作为输出。
Use: 采用:
mylist = [['Local Interface', '', 'Parent Interface', '', 'Chassis Id', '', '', '', '', 'Port info', '', '', '', '', 'System Name'], ['ge-0/0/1', '', '', '', '', ' -', '', '', '', '', '', '', '', '', ' 00:2d:b3:c9:e2:f0', ' 606', '', '', '', '', '', '', '', 'myhost-re01', '', '', '', ''], ['ge- 4/0/7', '', '', '', '', ' -', '', '', '', '', '', '', '', '', ' 00:2d:b3:c9:e2:f0', ' 628', '', '', '', '', '', '', '', 'myhost-re01', '', '', '', ''], ['ge-4/0/6', '', '', '', '', ' -', '', '', '', '', '', '', '', '', ' 00:2d:b3:c9:e2:f0', ' 629', '', '', '', '', '', '', '', 'myhost-re01', '', '', '', ''], ['ge-0/0/4', '', '', '', '', ' -', '', '', '', '', '', '', '', '', ' 00:2d:b3:c9:e2:f0', ' 138739712', '', '', '', '', 'PE12XC1010', '', '', '', ''], ['ge-0/0/2', '', '', '', '', ' -', '', '', '', '', '', '', '', '', ' 00:2d:b3:c9:e2:f0', ' gei_1/5', '', '', '', '', '', 'PE13XC1011', '', '', '', '', ''], ['ge-3/3/0', '', '', '', '', ' -', '', '', '', '', '', '', '', '', ' 0c:12:12:c7:c1:f7', ' gei_2/3', '', '', '', '', '', 'PEUTV01-01XT', '', '', ''], ['ge-3/3/4', '', '', '', '', ' - ', '', '', '', '', '', '', '', '', ' f0:1c:2d:22:37:c0', ' 783', '', '', '', '', '', '', '', 'myhost-re01', ''], ['{master}']]
mylist = list(map(lambda x: list(filter(None,x)), mylist))
print(mylist)
Output: 输出:
[['Local Interface', 'Parent Interface', 'Chassis Id', 'Port info', 'System Name'], ['ge-0/0/1', ' -', ' 00:2d:b3:c9:e2:f0', ' 606', 'myhost-re01'], ['ge- 4/0/7', ' -', ' 00:2d:b3:c9:e2:f0', ' 628', 'myhost-re01'], ['ge-4/0/6', ' -', ' 00:2d:b3:c9:e2:f0', ' 629', 'myhost-re01'], ['ge-0/0/4', ' -', ' 00:2d:b3:c9:e2:f0', ' 138739712', 'PE12XC1010'], ['ge-0/0/2', ' -', ' 00:2d:b3:c9:e2:f0', ' gei_1/5', 'PE13XC1011'], ['ge-3/3/0', ' -', ' 0c:12:12:c7:c1:f7', ' gei_2/3', 'PEUTV01-01XT'], ['ge-3/3/4', ' - ', ' f0:1c:2d:22:37:c0', ' 783', 'myhost-re01'], ['{master}']]
With list comprehension and filtering out those items of your sublist that equal to an empty string 使用列表理解并过滤出子列表中等于空字符串的那些项
[[sl for sl in sublist if sl != ''] for sublist in mylist]
Loops over your list 遍历您的清单
[.. for sublist in mylist]
and for each sublist only takes those that are not ''
而对于每个子列表,只接受那些不是
''
[sl for sl in sublist if sl != '']
If you want to use filter
this would be 如果您想使用
filter
这将是
[filter(lambda x: x != '', sublist) for sublist in mylist]
you can use filter
function in list comprehensions: 您可以在列表推导中使用
filter
功能:
mylist = mylist = [['Local Interface', '', 'Parent Interface', '', 'Chassis Id', '',
'', '', '', 'Port info', '', '', '', '', 'System Name'], ['ge-0/0/1', '',
'', '', '', '', '', '', '', '', '', '', ''], ['{master}']]
new_list = [list(filter(None,n)) for n in mylist]
print(new_list)
output 输出
[['Local Interface', 'Parent Interface', 'Chassis Id', 'Port info', 'System Name'], ['ge-0/0/1'], ['{master}']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.