我上线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()
始终显示完整的错误消息。 – furas
作为一个头,你正在重复使用'行'在每个for循环,应该是唯一的每一个。 – CasualDemon
一行中可以有多个IP地址吗? – tdelaney