如何将键移入值并在字典中作为键进行索引。我写了程序,工作正常。但是我需要枚举输出并将键转换为值并将索引号作为我的键

   data = '''
    120.115.144.240 - - [29/Aug/2017:04:40:03 -0400] "GET /apng/assembler-2.0/assembler2.php HTTP/1.1" 200 231 "http://littlesvr.ca/apng/history.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"

    202.167.250.99 - - [29/Aug/2017:04:41:10 -0400] "GET /apng/images/o_sample.png?1424751982?1424776117 HTTP/1.1" 200 115656 "http://bbs.mydigit.cn/read.php?tid=2186780&fpage=3" "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"

    120.115.144.240 - - [29/Aug/2017:04:40:03 -0400] "GET /apng/assembler-2.0/assembler2.php HTTP/1.1" 200 231 "http://littlesvr.ca/apng/history.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"

    60.4.236.27 - - [29/Aug/2017:04:42:46 -0400] "POST /apng/images/o_sample.png?1424751982?1424776117 HTTP/1.1" 404 115656 "http://bbs.mydigit.cn/read.php?tid=1952896" "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36"

    120.115.144.240 - - [29/Aug/2017:04:40:03 -0400] "POST /apng/assembler-2.0/assembler2.php HTTP/1.1" 404 231 "http://littlesvr.ca/apng/history.html" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36"'''


我的密码

    import re
    data = [line.strip() for line in data.splitlines() if line.strip()]
    out = {}
    for line in data:
        g = re.findall(r'([\d.]+).*?(GET|POST).*?"\s(200|404)', line)
        if not g:
            continue
        ip_address, method, status_code = g[0]
        out.setdefault(ip_address, {})
        out[ip_address].setdefault('404', 0)
        out[ip_address].setdefault('200', 0)
        out[ip_address].setdefault('GET', 0)
        out[ip_address].setdefault('POST', 0)
        out[ip_address][method] += 1
        out[ip_address][status_code] += 1
    from pprint import pprint
    pprint(out, width=30)


我的出在下面

{'1.1.1.1': {'200': 2,
                     '404': 1,
                     'GET': 2,
                     'POST': 1},
 '2.2.2.2': {'200': 1,
                    '404': 0,
                    'GET': 1,
                    'POST': 0},
 '6.3.3.3': {'200': 0,
                 '404': 1,
                 'GET': 0,
                 'POST': 1}}


我想要的是

{1:    {'IP': 120.115.144.240,
             '200': 2,
                     '404': 1,
                     'GET': 2,
                     'POST': 1},
 2:      {'IP':202.167.250.99,
                    '200': 1,
                    '404': 0,
                    'GET': 1,
                    'POST': 0},
 3:      {'IP':60.4.236.27:
                  '200': 0,
                 '404': 1,
                 'GET': 0,
                 'POST': 1}}

最佳答案

使用enumerate

例如:

import re
data = [line.strip() for line in data.splitlines() if line.strip()]
out = {}
for ind, line in enumerate(data, 1):
    g = re.findall(r'([\d.]+).*?(GET|POST).*?"\s(200|404)', line)
    if not g:
        continue
    ip_address, method, status_code = g[0]
    out.setdefault(ind, {"IP": ip_address})
    out[ind].setdefault('404', 0)
    out[ind].setdefault('200', 0)
    out[ind].setdefault('GET', 0)
    out[ind].setdefault('POST', 0)
    out[ind][method] += 1
    out[ind][status_code] += 1
from pprint import pprint
pprint(out, width=30)

08-27 17:19