如何读取所有IP地址在一个文本文件,并遍历每个IP地址只有一次
我上线re.search
错误。如何读取所有IP地址在一个文本文件,并遍历每个IP地址只有一次
def search(pattern, string, flags=0):
"""Scan through string looking for a match to the pattern, returning
a match object, or None if no match was found."""
return _compile(pattern, flags).search(string)
代码:
def IP():
file = open('mbox.txt' , 'r')
count = 0
for line in file:
address = re.search(r"\b\d{1,3}\. \d{1,3}\. d{1,3}\. \d{1,3}\b", file)
for line in address:
ip = address
if line != allIPS:
ip.add(ip)
ip.add('\n')
count = count +1
return (count)
def main():
#global statement for fhand
print("This program does the folowing: ")
print("The sum of lines in the file: %d " % (lineCount()))
print("The number of messages in the file: %d " % (MsgCount()))
print("All IP Addresses: %d\n " % (IP() ))
if __name__ == '__main__':
main()
我看到一个错误 - 你需要line
而不是file
在re.search()
你的正则表达式中有多余的空间,将让你从匹配点IPv4地址和你如何尝试迭代线路有问题。试试这个:
def IP():
# use a set to maintain unique addresses. no need to check if the
# address is in the set because duplicates are automatically removed
# during add.
allIPS = set()
# open with a context manager for automatic close. You dont need to
# specify the mode because "r" is the default.
with open('mbox.txt') as myfile:
# now iterate the lines
for line in myfile:
# use findall to find all matches in the line and
# update the set
allIPS.update(
re.findall(r"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b",
line))
# it seems like all you care about is the number of unique addresses
return len(allIPS)
非常感谢!但现在当我运行该程序所有IP地址只是给了我0. –
看起来像正则表达式中的一个缺少反斜杠。我已经修复了代码。 – tdelaney
我不想成为一个痛苦,但我仍然得到相同的结果 –
始终显示完整的错误消息。 – furas
作为一个头,你正在重复使用'行'在每个for循环,应该是唯一的每一个。 – CasualDemon
一行中可以有多个IP地址吗? – tdelaney