简体   繁体   English

如何清除None在我的输出中键入-我只想选择有效的IP地址

[英]How to get rid of None Type in my output - I only want to select valid IP addresses

Any idea how to not include anything with None? 任何想法如何不包含任何与无? I am trying to just pull in IP addresses at this point, but I don't want to include empty elements. 我现在只想输入IP地址,但是我不想包含空元素。

My API Response 我的API回应

[{'name': '', 'serial': 'Q2KN-xxxx-438Z', 'mac': '0c:8d:db:c3:ad:c8', 'networkId': 'L_6575255xxx96096977', 'model': 'MX64', 'address': '', 'lat': 38.4180951010362, 'lng': -92.098531723022, 'notes': '', 'tags': '', 'wan1Ip': '47.134.13.195', 'wan2Ip': None}, {'name': '', 'serial': 'Q2PD-xxx-QQ9Y', 'mac': '0c:8d:db:dc:ed:f6', 'networkId': 'L_657525545596096977', 'model': 'MR33', 'address': '', 'lat': 38.4180951010362, 'lng': -92.098531723022, 'notes': '', 'tags': '', 'lanIp': '10.0.0.214'}]

Iterating through elements and selecting certain fields 遍历元素并选择某些字段

response = requests.request("GET", url + id + '/devices', headers=headers)
data = response.json()
for item in data:
  keys = [item.get(x) for x in ['wan1Ip', 'model', 'lanIp', 'wan2Ip']]
  print(*keys, sep="\n", file=sys.stdout)

My output is: 我的输出是:

47.134.13.195
MX64
None
None
None
MR33
10.0.0.214
None

My desired output is: 我想要的输出是:

47.134.13.195
10.0.0.214

I've tried adding a re.findall for ip addresses, but not sure that's going to work for me. 我尝试为IP地址添加一个re.findall,但不确定是否对我有用。 I've also tried to add operators for not in None and several other things. 我还尝试为not in None和其他一些事情添加运算符。

re.findall(“(?:[\\d]{1,3}).(?:[\\d]{1,3}).(?:[\\d]{1,3}).(?:[\\d]{1,3})?“,string2 ) re.findall(“(?:[\\ d] {1,3})。(?:[\\ d] {1,3})。(?:[\\ d] {1,3})。(?: [\\ d] {1,3})?“,string2)

Update I've changed my line to keys = [item.get(x) for x in ['wan1Ip', 'model', 'lanIp', 'wan2Ip', '{}']if x in item] 更新我已将行更改为keys = [item.get(x) for x in ['wan1Ip', 'model', 'lanIp', 'wan2Ip', '{}']if x in item]变为keys = [item.get(x) for x in ['wan1Ip', 'model', 'lanIp', 'wan2Ip', '{}']if x in item]

Obviously, I still have non IP addresses in my output, but I can select the elements that have IP addresses only. 显然,我的输出中仍然有非IP地址,但是我可以选择仅具有IP地址的元素。 My main issue was None. 我的主要问题是无。 I will also try some of the other suggestions. 我还将尝试其他一些建议。

Add a filter to your list comprehension to check to see if key x is in item . 将过滤器添加到列表理解中,以检查键x是否在item

for item in data:
  keys = [item.get(x) for x in ['wan1Ip', 'model', 'lanIp', 'wan2Ip'] if x in item]
  print(*keys, sep="\n", file=sys.stdout)

Search each key ipv4 with regex( https://stackoverflow.com/a/5284410/6250402 ) and check key is None with item.get(x) or '' 使用正则表达式( https://stackoverflow.com/a/5284410/6250402 )搜索每个ipv4密钥,并使用item.get(x) or ''检查密钥是否为None item.get(x) or ''

import re

myRegex = re.compile(r'\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\.|$)){4}\b')
# data here

for item in data:

    keys = [item.get(x) for x in ['wan1Ip', 'model', 'lanIp', 'wan2Ip'] if myRegex.search(item.get(x) or '')]
    print(*keys, sep="\n", file=sys.stdout)

You could add this condition to the end of your list comprehension: 您可以将此条件添加到列表理解的末尾:

keys = [item.get(x) for x in ['wan1Ip', 'model', 'lanIp', 'wan2Ip'] if item.get(x) is not None]

The output will be what you want. 输出将是您想要的。

Could you try this: 您可以尝试一下:

    >> data = response.json()
    >> keys = [x.get(key) for key in ['wan1Ip', 'model', 'lanIp', 'wan2Ip'] for x in data if x.get(key)]
    >> print(keys)
    >> ['47.134.13.195', 'MX64', 'MR33', '10.0.0.214']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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