Reputation: 3
I need to do something like this with regex:
'host': '146.204.224.152',
'user_name': 'feest6811',
'time': '21/Jun/2019:15:45:24 -0700',
'request': 'POST /incentivize HTTP/1.1'
I already have the four lists:
host = re.findall("(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})",logdata)
user_name = re.findall("[a-z]{1,100}\d{4}\ ", logdata)
time = re.findall("\d{2}/[A-z]{3}/\d{4}:\d{2}:\d{2}:\d{2} -\d{4}", logdata)
request = re.findall("[A-Z]{1,30}\ /[\w/+]{1,100} [HTTP/]{5}\d{1}\.\d{1}", logdata)
And then tried to make a dictionary in this way:
result = {'host': host, 'user_name': user_name, 'time': time, 'request': request}
But I got this:
'host': ['146.204.224.152',
'197.109.77.178',
'156.127.178.177',
'100.32.205.59',
'168.95.156.240',
'71.172.239.195',
'180.95.121.94',
'144.23.247.108',
'2.179.103.97',
'241.114.184.133',
...]
'user_name': ['feest6811 ',
'kertzmann3129 ',
'okuneva5222 ',
'ortiz8891 ',
'stark2413 ',
'dooley1853 ',
'mohr6893 ',
'auer7552 ',
'lind8584 ',
'tromp8355 ',
'keebler1423 ',
'klein8508 ',
'gusikowski9864 ',
'medhurst2732 ',
'dubuque8645 ',
...]
and so on with the other varibles. How can I make it as the example?
Upvotes: 0
Views: 65
Reputation: 77910
You need to iterate through your findall
results in parallel. What you did was to make one large dict entry containing the entire lists.
elist = [] # short for entry list
for ehost, euser, etime, ereq in zip(host, user_name, time, request):
elist.append(
{"host": ehost,
"user_name": ename,
"time": etime,
"request": ereq
}
Perhaps more to your comfort level would be to use a common index, and pull each corresponding value out of each list:
for i in range(len(host)):
elist.append(
{"host": host[i],
"user_name": user_name[i],
"time": time[i],
"request": req[i]
}
Upvotes: 1