我需要别人的帮助。有一个包含某些字段的数据库输出日志文件。只需要提取具有IP地址(#1100:“)和日期(”#907:“)的字段。 之后,我需要删除一个doublet地址并按日期对其进行排序。我做不到。
ip = []
date = []
uniq_ip = {}
file2 = open("list_of_ip.txt", "w")
file3 = open("uniq_ip.txt", "w")
# opening and reading log file
# adding ip and date to lists
with open("db_output.txt", "r", encoding='utf-8') as file:
for line in file:
line = line.split()
if line[0] == '#1100:':
ip.append(line[1])
file2.write(line[1] + "-")
if line[0] == '#907:':
date.append(int(line[1]))
file2.write(line[1] + "\n")
# opening and file with ip and dates (dates like strings, not date object) log file
# adding ip and date to lists
with open("list_of_ip.txt") as file:
for line in file:
ip = line.split("-")[0]
date = line.split("-")[1]
uniq_ip[ip] = date
# try sorting a dict by value
sorted_by_value = sorted(uniq_ip.items(), key=lambda kv: kv[1])
for elem in sorted_by_value:
file3.write(elem[0] + " - " + elem[1])
我做了一些事情,但是没有用,无法排序。 谢谢。