我的代码不喜欢我要求它将某些信息附加到特定行。我不知道为什么
作为参考,我的代码如下所示:
def lookOnMe(fileName):
import socket
global HostName
with open(fileName, 'r+') as f:
next(f)
for line in f:
f.seek(0)
print(line)
findStop = line.index("|")
remainder = line[findStop+2:]
findStop = remainder.index("|")
ipAdd = remainder[:findStop]
ipAdd = "".join(ipAdd.split())
try:
HostName = socket.gethostbyaddr(ipAdd)
except:
HostName = '-'
f.write(line.rstrip('\n') + " | " + HostName + '\n')
f.read()
和输入字符串,如下所示:
5558 | 1.000.00.00 | 1.222.0.0/10 | ER | apnic | 2001-07-19 | SOMESTUFF
很简单
您的
next(f)
语句只读取一行,即文件中的第一行,而不对其执行任何操作然后,您第一次执行
for line in f
,它尝试读取第二行。这将是空的(因为在第一行之后可能有一个换行符)它无法使用子字符串检测解析空行,因为不存在
|
字符或任何类似的字符相关问题 更多 >
编程相关推荐