Advertisement
Guest User

Second Program

a guest
Feb 19th, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.08 KB | None | 0 0
  1. import re
  2. from pprint import pprint
  3. from datetime import datetime
  4. f = open("raw.txt","r").readlines()
  5. final = []
  6. for data in f[1:]:
  7.     data = data.strip()
  8.     result = []
  9.     matches = re.finditer(r'(\[[^"]*\])+|[^" ]+|("[^"]*")', data.strip())
  10.     for matchNum, match in enumerate(matches):
  11.         matchNum = matchNum + 1
  12.         result.append(match.group())
  13.     final.append(result)
  14.    
  15. hasil = []
  16. for x in final:
  17.     url = x[5].split(" ")[1]
  18.     date, time = x[0].replace("[","").replace("]","").split(':', maxsplit=1)
  19.     date = datetime.strptime(date, '%d/%b/%Y').strftime('%Y-%m-%d')
  20.     hasil.append({
  21.         "date": date,
  22.         "time": time[:8],
  23.         "dest_ip": x[2],
  24.         "src_ip": x[3],
  25.         "status_code": x[4],
  26.         "url": url
  27.     })
  28.  
  29. # simpan ke csv file
  30. simpan = open("hasil.csv","w")
  31. # add header
  32. simpan.write("{0},{1},{2},{3}\n".format("Date", "Time", "Destip","Srcip"))
  33. for x in hasil:
  34.     # tulis detail
  35.     simpan.write("{0},{1},{2},{3}\n".format(str(x['date']), str(x['time']), str(x['dest_ip']), str(x['src_ip'])))
  36. simpan.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement