2021年3月26日星期五

I try to parse line by line within a text file in Python , but it just can parse first line, it can not read from second line

My Text file 'test.txt' looks like;

hostname Switch

network 192.168.8.0 255.255.255.0

===================================================

My python coding "TextFileParse.py" ;

import os

f = open('test.txt', 'r')

while 1:

lines = f.readlines()    for line in lines:      item = line.split()      print(line)      name = item[item.index('hostname')+1]      nw = item[item.index('network')+1]      print(name, nw)    

f(close)

The result message ;

hostname Switch

Traceback (most recent call last): File "D:\TextFileParse.py", line 15, in nw = item[item.index('network')+1] ValueError: 'network' is not in list

How can I read the index from second line ? I am not Program expert, just beginner.

https://stackoverflow.com/questions/66827588/i-try-to-parse-line-by-line-within-a-text-file-in-python-but-it-just-can-parse March 27, 2021 at 10:50AM

没有评论:

发表评论