首页 > 解决方案 > 忽略 re 上的引号

问题描述

我试图将我的数据转换为字典列表,例如

example_dict = {"host":"146.204.224.152", 
                "user_name":"feest6811", 
                "time":"21/Jun/2019:15:45:24 -0700",
                "request":"POST /incentivize HTTP/1.1"} #note: not everything is a POST

我的数据:

146.204.224.152 - feest6811 [21/Jun/2019:15:45:24 -0700] "POST /incentivize HTTP/1.1" 302 4622
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
144.23.247.108 - auer7552 [21/Jun/2019:15:45:35 -0700] "POST /extensible/infrastructures/one-to-one/enterprise HTTP/1.1" 100 22921

我的代码:

pattern = """
(?P<host>.*)     #User host
(-\ )            #Separator
(?P<user_name>\w*) #User name
(\ \[)             #Separator for pharanteses and space
(?P<time>\S*\ -0700) #time
(\]\ )             #Separator for pharanteses and space
(?P<request>\w* ")
"""
for user in re.finditer(pattern,logdata,re.VERBOSE):
    print(user.groupdict())

输出:在此处输入图像描述

我无法摆脱引号。试图添加\"或只是"到分隔线,但后来我无法得到任何输出。

标签: pythonre

解决方案


推荐阅读