【问题标题】:how to read a routing table from a file in python如何从python中的文件中读取路由表
【发布时间】:2013-01-28 07:16:06
【问题描述】:

我想从 python 中的文本文件中读取 ip 地址 128.11.56.0,255.255.255.0,117,9,45,16 并将它们存储在变量中。即子网、掩码和下一跳。

【问题讨论】:

  • 请查看faqs。通常在您的项目中添加有问题的代码并就特定问题提出特定问题。

标签: python-3.x python-2.7


【解决方案1】:

如果您的文件具有以下格式:

(第 1 行):子网、掩码、nexttop
(第 2 行):子网、掩码、nexttop
...
(第 n 行):子网、掩码、nexttop

f = open(path_to_file)
lines = f.readlines()
data = []
for line in lines:
    o = {"subnet" : line.split(',')[0], "mask" : line.split(',')[1], "nexthop" : line.split(',')[2]}
    data.append(o)

【讨论】:

  • 感谢您的回复,但是当我运行此代码时出现问题,它向我显示错误,列表索引超出范围 Traceback(最近一次调用最后一次):文件“dat.py”,第 10 行, in words={"subnet":line.split(',')[0],"mask":line.split(',')[1],"nexthope":line.split(', ')[2]} IndexError: list index out of range rana@ubuntu:~$
  • 您确定您的输入文件格式完全符合以下格式:ip1,ip2,ip3 per line 吗?
猜你喜欢
  • 1970-01-01
  • 2017-08-29
  • 2012-11-12
  • 2022-10-24
  • 2013-12-18
  • 2019-06-04
  • 2016-10-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多