【发布时间】:2021-03-08 01:02:42
【问题描述】:
import re
filename = "access.log"
path = ""
with open (path + filename, "r") as logfile:
count = 0
for line in logfile: # Loops through the log file
regex = ('(?:(GET|POST) )(\S+)') # Stores the regex
url = re.findall(regex, line) # Uses the findall method and stores it in url variable
print(url[0][1]) # Prints out a list of URLs
这是一个日志文件的例子
access.log
209.160.24.63 - - [01/Feb/2021:18:22:17] "GET /product.screen?productId=BS-AG-G09&JSESSIONID=SD0SL6FF7ADFF4953 HTTP 1.1" 200 2550 " http://www.google.com/productid=12wdef" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.46 Safari/536.5" 422
我得到了粗体的 URL,但我现在想将其拆分并存储在 python 的字典中。
【问题讨论】:
标签: python regex dictionary