Мне кажется, что ip указан первым.Это означает, что вы можете прочитать файл построчно и вычеркнуть первое слово, являющееся ip, и добавить его в список для возврата.Если вы хотите сделать более продвинутый поиск / фильтрацию, я бы предложил отформатировать этот журнал, читая и записывая этот файл журнала, в формат типа xml или json.Это будет легче искать, чем блоки строк.
def append_log_storage(file, storage_file):
root_elm = ET.Element('root')
# Load file to memory
log_content = None
with open(file) as f:
log_content = f.readlines()
f.close()
# Write to file
f = open(file, 'w')
for line in log_content:
if line == '\n':
continue
ip = line.partition(' ')[0]
ip_log_elm = ET.SubElement(root_elm, 'log')
ip_elm = ET.SubElement(ip_log_elm, 'ip')
# date_elm, etc. Add any other data for the log element
ip_elm.text = ip
f.close()
xml_data = ET.tostring(root_elm)
xml_file = open(storage_file, 'wb')
xml_file.write(xml_data)
def read_log_details(file):
# Read file by line
ip_list = []
with open(file) as f:
for line in f:
if line == '\n':
continue
ip = line.partition(' ')[0]
ip_list.append(ip)
f.close()
return ip_list
read_log_details('log.txt')
#append_log_storage('log.txt', 'log.xml')
https://stackabuse.com/reading-and-writing-xml-files-in-python/