简体   繁体   中英

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.

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. For example lldp info output to be add onto "lldpinfo" or 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...

"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(lambda x: x != '', sublist) for sublist in mylist]

you can use filter function in list comprehensions:

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}']]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM