Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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
使用python3如何从以下格式获取总可用地址:_Python_Python 3.x - Fatal编程技术网

使用python3如何从以下格式获取总可用地址:

使用python3如何从以下格式获取总可用地址:,python,python-3.x,Python,Python 3.x,使用python3如何从以下格式(即os.system()调用的标准输出)中获取总可用地址: 如果您有任何建议,我们将不胜感激。假设output\u lines是来自os.system()调用的行列表: def find_usable_addresses(output_lines): for line in output_lines: if line.startswith('Usable IP Addresses'): value = line.split(': ')[1

使用python3如何从以下格式(即os.system()调用的标准输出)中获取总可用地址:


如果您有任何建议,我们将不胜感激。

假设
output\u lines
是来自
os.system()
调用的行列表:

def find_usable_addresses(output_lines):
  for line in output_lines:
    if line.startswith('Usable IP Addresses'):
      value = line.split(': ')[1] # split to ('Usable...', '254') 
      return int(value)

如果找不到值,此函数将返回
None

问题在于
os.system()
不会返回它运行的命令的输出流。请尝试改用:
output\u lines=os.popen('that command').readlines()
抱歉,我很难将我的评论格式化并包含代码。这一切都以一行字结束。感谢您的帮助,我将尝试os.popen()。好的,我取得了一些进展,但我得到了以下错误值错误:以10为基数的int()的文本无效:“65534\n”我是否需要以某种方式丢失逗号?请使用
value.strip()
删除尾随的换行符。请澄清您试图执行的操作。您是否需要从示例中的“可用IP地址=……:254”行中提取数字254?是的,提取总可用地址(254、65534等),如果有逗号,则将其去掉,以便对提取的数字进行计算。
def find_usable_addresses(output_lines):
  for line in output_lines:
    if line.startswith('Usable IP Addresses'):
      value = line.split(': ')[1] # split to ('Usable...', '254') 
      return int(value)