我上線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