Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python删除嵌套列表中的空引号无效_Python_List_Csv_Dictionary - Fatal编程技术网

使用python删除嵌套列表中的空引号无效

使用python删除嵌套列表中的空引号无效,python,list,csv,dictionary,Python,List,Csv,Dictionary,我制作了一个简单的脚本来测试如何从列表中删除空引号。输出实际上来自设备输出,我将输出保存到csv文件中 这是我的密码 mylist = [['Local Interface', '', 'Parent Interface', '', 'Chassis Id', '', '', '', '', 'Port info', '', '', '', '', 'System Name'], ['ge-0/0/1', '', '', '', '', ' -', '', '', '', '', ''

我制作了一个简单的脚本来测试如何从列表中删除空引号。输出实际上来自设备输出,我将输出保存到csv文件中

这是我的密码

 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)
过滤前后的打印结果相同

似乎很容易解决,但到目前为止还没有成功…希望有人能帮助我。谢谢

&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& 更新1: 我有下面的json api,需要用值进行解析。例如,要添加到lldpinfo或数据上的lldp info输出[2]

json1 = {
  "channel": "scanner",
  "action": "device_scan",
  "table": "D2",
  "device":[]
}
data = "hostname","ipaddress","lldpinfo"
解析值时的总体示例结果如下所示

{
  "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": [
      {
      }
     ]
    }
  ]
}
从上面的结果来看,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"
   }
  ]

您可以使用嵌套列表理解从嵌套列表中排除虚假项(在本例中为空字符串)

mylist = [[x for x in line if x] for line in mylist]

这是一个列表:

[x for x in line if x]
嵌套在另一个内部:

[... for line in mylist]

将嵌套列表作为输出。

具有列表理解功能,并过滤掉子列表中等于空字符串的项目

[[sl for sl in sublist if sl != ''] for sublist in mylist]
循环浏览您的列表

[.. for sublist in mylist]
对于每个子列表,只取那些不是

如果您想使用过滤器,这将是

[filter(lambda x: x != '', sublist) for sublist in mylist]
使用:

输出:

您可以在列表理解中使用筛选函数:

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)
输出


您还可以过滤None、False和0@ezdazuzena这是一个嵌套的字符串列表,不是None、False和0。它与FilterOne具有相同的效果,…嗨…此方法也有效。非常感谢。使用理解运算符和lambda运算符有什么不同之处?@chenoi在Python3中,filter和map不返回列表。它们返回可转换为列表的可编辑对象。列表理解是一种纯粹用于以透明方式构建列表的语法。使用你觉得更可读的。好的..明白了..“列表理解是一种纯粹用于以透明方式构建列表的语法”。。。。它是否关心列表是否嵌套?谢谢你…我得到[,,,]更改答案可能删除第一个。。。。列表中的['Local Interface'、'Parent Interface'、'Chassis Id'、'Port info'、'System Name']、empty[]和['{master}']?抱歉,我不明白…是否要展平列表?否…如果列表为空[],并且在本例中需要删除的特定列表['Local Interface'、'Parent Interface'、'Chassis Id'、'Port info'、'System Name'],空[]和['{master}']…列表切片printmylist[1:-1]?它确实删除['{master}']。。。所以可以使用切片…来删除[‘本地接口’、‘父接口’、‘机箱Id’、‘端口信息’、‘系统名称’]或任何空[]?
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)
[['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 = 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)
[['Local Interface', 'Parent Interface', 'Chassis Id', 'Port info', 'System Name'], ['ge-0/0/1'], ['{master}']]