我有以下txt文件:
197.109.77.178 - kertzmann3129 [21/Jun/2019:15:45:25 -0700] "DELETE /virtual/solutions/target/web+services HTTP/2.0" 203 26554
156.127.178.177 - okuneva5222 [21/Jun/2019:15:45:27 -0700] "DELETE /interactive/transparent/niches/revolutionize HTTP/1.1" 416 14701
100.32.205.59 - ortiz8891 [21/Jun/2019:15:45:28 -0700] "PATCH /architectures HTTP/1.0" 204 6048
168.95.156.240 - stark2413 [21/Jun/2019:15:45:31 -0700] "GET /engage HTTP/2.0" 201 9645
71.172.239.195 - dooley1853 [21/Jun/2019:15:45:32 -0700] "PUT /cutting-edge HTTP/2.0" 406 24498
180.95.121.94 - mohr6893 [21/Jun/2019:15:45:34 -0700] "PATCH /extensible/reinvent HTTP/1.1" 201 27330
我想创建一个函数,将它们转换为多个字典,其中每一行都是一个字典:
example_dict = {"host":"146.204.224.152", "user_name":"feest6811", "time":"21/Jun/2019:15:45:24 -0700", "request":"POST /incentivize HTTP/1.1"}
到目前为止,我能够做到这一点,为所有项目创建 4 个列表,但我不知道如何为每行创建多个 dic:
import re
def logs():
with open("assets/logdata.txt", "r") as file:
logdata = file.read()
host = (re.findall('(.*?)\-',logdata))
username = re.findall('\-(.*?)\[',logdata)
time = re.findall('\[(.*?)\]', logdata)
request = re.findall('\"(.*?)\"',logdata)
#for line in range(len(logdata)):
#dc = {'host':host[line], 'user_name':user_name[line], 'time':time[line], 'request':request[line]}
慕斯709654
料青山看我应如是
跃然一笑
莫回无
FFIVE
相关分类