[英]Pulling specific words out of a line of a text file in python
我是一名新手python程序员,正在做一些网络编码工作。 为了检查是否已建立到路由器默认网关的连接,我想从我的ipaddr文件中提取该网关并将其转换为一个变量,但是我无法获取它来从特定的列表中提取该列行,只有第一行。
到目前为止,这是我的代码:
with open('ipaddr.txt', "r") as f:
line=f.readlines()
columns = [line.rstrip("\t") for line in f]
print (line[22])
print (line[18])
print (line[26])
f.close()
我希望它从以上各行的第39列向前拉。
您正在使用rstrip
,而应该使用split
。 您还将读取文件两次readlines()
和for line in f
。
with open('ipaddr.txt', "r") as f:
lines=f.readlines()
table = [line.split("\t") for line in lines]
print table[22][39:]
print table[18][39:]
print table[26][39:]
f.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.