Python .append()到一行中的向量

时间:2016-10-13 08:29:12

标签: python parsing nginx python-textprocessing

我在数组append遇到问题。我期待的结果如下:

['44229#0:', '2016/10/11', '11:15:57','11:15:57','11:15:57','11:15:58' '0']

但我有这个结果:

['44229#0:', '2016/10/11', '11:15:57']
['11:15:57']
['11:15:57']
['11:15:58']
['44231#0:', '2016/10/11', '11:19:23']
['11:19:23']
['1']
['11:19:24']
['11:19:24']
['44231#0:', '2016/10/11', '12:20:39']
['12:20:58']
['12:20:59']
['12:20:59']

如果我稍微缩进输出,我会得到这样的结果:

['44229#0:', '2016/10/11', '11:15:57', '11:15:57', '11:15:57', '11:15:58', '44231#0:', '2016/10/11', '11:19:23', '11:19:23', '1', '11:19:24', '11:19:24', '44231#0:', '2016/10/11', '12:20:39', '12:20:58', '12:20:59', '12:20:59']

仍然不是我需要的那些,因为我在一个载体中都有,并且需要将它们分成不同的行...

我正在从Nginx解析此日志:

2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL_do_handshake: -1
2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL_get_error: 2
2016/10/11 11:15:57 [debug] 44229#0: *45677 post event 0000000001449060
2016/10/11 11:15:57 [debug] 44229#0: *45677 delete posted event 0000000001449060
2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL handshake handler: 0
2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL_do_handshake: 1
2016/10/11 11:15:57 [debug] 44229#0: *45677 http check ssl handshake
2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL: TLSv1.2, cipher:
2016/10/11 11:15:57 [debug] 44229#0: *45677 reusable connection: 1
2016/10/11 11:15:57 [debug] 44229#0: *45677 http wait request handler
2016/10/11 11:15:57 [debug] 44229#0: *45677 malloc: 00000000014456D0:1024
2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL_read: -1
2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL_get_error: 2
2016/10/11 11:15:57 [debug] 44229#0: *45677 free: 00000000014456D0
2016/10/11 11:15:57 [debug] 44229#0: *45677 post event 0000000001449060
2016/10/11 11:15:57 [debug] 44229#0: *45677 delete posted event 0000000001449060
2016/10/11 11:15:57 [debug] 44229#0: *45677 http wait request handler
2016/10/11 11:15:57 [debug] 44229#0: *45677 malloc: 00000000014456D0:1024
2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL_read: 144
2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL_read: -1
2016/10/11 11:15:57 [debug] 44229#0: *45677 SSL_get_error: 2
2016/10/11 11:15:57 [debug] 44229#0: *45677 reusable connection: 0
2016/10/11 11:15:57 [debug] 44229#0: *45677 posix_memalign: 00000000014974A0:4096 @16
2016/10/11 11:15:57 [debug] 44229#0: *45677 http process request line
2016/10/11 11:15:57 [debug] 44229#0: *45677 http request line: "POST / HTTP/1.1"
2016/10/11 11:15:57 [debug] 44229#0: *45677 http uri: "/"
2016/10/11 11:15:57 [debug] 44229#0: *45677 http args: ""
2016/10/11 11:15:57 [debug] 44229#0: *45677 http exten: ""
2016/10/11 11:15:57 [debug] 44229#0: *45677 http process request header line
2016/10/11 11:15:57 [debug] 44229#0: *45677 http header: "Host:"
2016/10/11 11:15:57 [debug] 44229#0: *45677 http header: "Transfer-Encoding: chunked"
2016/10/11 11:15:57 [debug] 44229#0: *45677 http header: "Accept: */*"
2016/10/11 11:15:57 [debug] 44229#0: *45677 http header: "content-type: application/json"
2016/10/11 11:15:57 [debug] 44229#0: *45677 posix_memalign: 00000000016689D0:4096 @16
2016/10/11 11:15:57 [debug] 44229#0: *45677 http header: "Content-Length: 149"
2016/10/11 11:15:58 [debug] 44229#0: *45677 post event 0000000001449060
2016/10/11 11:15:58 [debug] 44229#0: *45677 delete posted event 0000000001449060
2016/10/11 11:15:58 [debug] 44229#0: *45677 http process request header line
2016/10/11 11:15:58 [debug] 44229#0: *45677 SSL_read: 6
2016/10/11 11:15:58 [debug] 44229#0: *45677 SSL_read: 149
2016/10/11 11:15:58 [debug] 44229#0: *45677 SSL_read: 7
2016/10/11 11:15:58 [debug] 44229#0: *45677 SSL_read: -1
2016/10/11 11:15:58 [debug] 44229#0: *45677 SSL_get_error: 2
2016/10/11 11:15:58 [debug] 44229#0: *45677 http header done
2016/10/11 11:15:58 [debug] 44229#0: *45677 event timer del: 43: 1476177405011
2016/10/11 11:15:58 [debug] 44229#0: *45677 generic phase: 0
2016/10/11 11:15:58 [debug] 44229#0: *45677 rewrite phase: 1
2016/10/11 11:15:58 [debug] 44229#0: *45677 test location: "/"
2016/10/11 11:15:58 [debug] 44229#0: *45677 using configuration "/"
2016/10/11 11:15:58 [debug] 44229#0: *45677 http cl:-1 max:1048576
2016/10/11 11:15:58 [debug] 44229#0: *45677 rewrite phase: 3
2016/10/11 11:15:58 [debug] 44229#0: *45677 http set discard body
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 39 s:0
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 35 s:1
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 0D s:1
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 0A s:3
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 7B s:4
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 0D s:5
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 0A s:6
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 30 s:0
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 0D s:1
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 0A s:8
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 0D s:9
2016/10/11 11:15:58 [debug] 44229#0: *45677 http chunked byte: 0A s:10
2016/10/11 11:15:58 [debug] 44229#0: *45677 xslt filter header
2016/10/11 11:15:58 [debug] 44229#0: *45677 HTTP/1.1 200 OK
2016/10/11 11:15:58 [debug] 44229#0: *45677 write new buf t:1 f:0 0000000001668BF0, pos 0000000001668BF0, size: 160 file: 0, size: 0
2016/10/11 11:15:58 [debug] 44229#0: *45677 http write filter: l:0 f:0 s:160
2016/10/11 11:15:58 [debug] 44229#0: *45677 http output filter "/?"
2016/10/11 11:15:58 [debug] 44229#0: *45677 http copy filter: "/?"
2016/10/11 11:15:58 [debug] 44229#0: *45677 image filter
2016/10/11 11:15:58 [debug] 44229#0: *45677 xslt filter body
2016/10/11 11:15:58 [debug] 44229#0: *45677 http postpone filter "/?" 00007FFFADE3C4A0
2016/10/11 11:15:58 [debug] 44229#0: *45677 write old buf t:1 f:0 0000000001668BF0, pos 0000000001668BF0, size: 160 file: 0, size: 0
2016/10/11 11:15:58 [debug] 44229#0: *45677 write new buf t:0 f:0 0000000000000000, pos 0000000000000000, size: 0 file: 0, size: 0
2016/10/11 11:15:58 [debug] 44229#0: *45677 http write filter: l:1 f:0 s:160
2016/10/11 11:15:58 [debug] 44229#0: *45677 http write filter limit 0
2016/10/11 11:15:58 [debug] 44229#0: *45677 posix_memalign: 0000000001499DB0:256 @16
2016/10/11 11:15:58 [debug] 44229#0: *45677 malloc: 000000000175B750:16384
2016/10/11 11:15:58 [debug] 44229#0: *45677 SSL buf copy: 160
2016/10/11 11:15:58 [debug] 44229#0: *45677 SSL to write: 160
2016/10/11 11:15:58 [debug] 44229#0: *45677 SSL_write: 160
2016/10/11 11:15:58 [debug] 44229#0: *45677 http write filter 0000000000000000
2016/10/11 11:15:58 [debug] 44229#0: *45677 http copy filter: 0 "/?"
2016/10/11 11:15:58 [debug] 44229#0: *45677 http finalize request: 0, "/?" a:1, c:1
2016/10/11 11:15:58 [debug] 44229#0: *45677 set http keepalive handler
2016/10/11 11:15:58 [debug] 44229#0: *45677 http close request
2016/10/11 11:15:58 [debug] 44229#0: *45677 http log handler
2016/10/11 11:15:58 [debug] 44229#0: *45677 free: 00000000014974A0, unused: 1
2016/10/11 11:15:58 [debug] 44229#0: *45677 free: 00000000016689D0, unused: 3109
2016/10/11 11:15:58 [debug] 44229#0: *45677 free: 00000000014456D0
2016/10/11 11:15:58 [debug] 44229#0: *45677 hc free: 0000000000000000 0
2016/10/11 11:15:58 [debug] 44229#0: *45677 hc busy: 0000000000000000 0
2016/10/11 11:15:58 [debug] 44229#0: *45677 free: 000000000175B750
2016/10/11 11:15:58 [debug] 44229#0: *45677 tcp_nodelay
2016/10/11 11:15:58 [debug] 44229#0: *45677 reusable connection: 1
2016/10/11 11:15:58 [debug] 44229#0: *45677 event timer add: 43: 65000:1476177423255
2016/10/11 11:17:03 [debug] 44229#0: *45677 event timer del: 43: 1476177423255
2016/10/11 11:17:03 [debug] 44229#0: *45677 http keepalive handler
2016/10/11 11:17:03 [debug] 44229#0: *45677 close http connection: 43
2016/10/11 11:17:03 [debug] 44229#0: *45677 SSL_shutdown: 1
2016/10/11 11:17:03 [debug] 44229#0: *45677 reusable connection: 0
2016/10/11 11:17:03 [debug] 44229#0: *45677 free: 0000000000000000
2016/10/11 11:17:03 [debug] 44229#0: *45677 free: 0000000000000000
2016/10/11 11:17:03 [debug] 44229#0: *45677 free: 00000000014462C0, unused: 8
2016/10/11 11:17:03 [debug] 44229#0: *45677 free: 000000000149ACF0, unused: 8
2016/10/11 11:17:03 [debug] 44229#0: *45677 free: 0000000001499DB0, unused: 144
2016/10/11 11:19:22 [debug] 44231#0: *45709 event timer add: 8: 60000:1476177622411
2016/10/11 11:19:22 [debug] 44231#0: *45709 reusable connection: 1
2016/10/11 11:19:22 [debug] 44231#0: *45709 epoll add event: fd:8 op:1 ev:80002001
2016/10/11 11:19:23 [debug] 44231#0: *45709 post event 0000000001448EE0
2016/10/11 11:19:23 [debug] 44231#0: *45709 delete posted event 0000000001448EE0
2016/10/11 11:19:23 [debug] 44231#0: *45709 http check ssl handshake
2016/10/11 11:19:23 [debug] 44231#0: *45709 http recv(): 1
2016/10/11 11:19:23 [debug] 44231#0: *45709 https ssl handshake: 0x16
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL_do_handshake: -1
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL_get_error: 2
2016/10/11 11:19:23 [debug] 44231#0: *45709 reusable connection: 0
2016/10/11 11:19:23 [debug] 44231#0: *45709 post event 0000000001448EE0
2016/10/11 11:19:23 [debug] 44231#0: *45709 delete posted event 0000000001448EE0
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL handshake handler: 0
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL_do_handshake: -1
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL_get_error: 2
2016/10/11 11:19:23 [debug] 44231#0: *45709 post event 0000000001448EE0
2016/10/11 11:19:23 [debug] 44231#0: *45709 delete posted event 0000000001448EE0
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL handshake handler: 0
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL_do_handshake: 1
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL: TLSv1.2, cipher:
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL reused session
2016/10/11 11:19:23 [debug] 44231#0: *45709 reusable connection: 1
2016/10/11 11:19:23 [debug] 44231#0: *45709 http wait request handler
2016/10/11 11:19:23 [debug] 44231#0: *45709 malloc: 00000000014E16E0:1024
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL_read: -1
2016/10/11 11:19:23 [debug] 44231#0: *45709 SSL_get_error: 2
2016/10/11 11:19:23 [debug] 44231#0: *45709 free: 00000000014E16E0
2016/10/11 11:19:24 [debug] 44231#0: *45709 post event 0000000001448EE0
2016/10/11 11:19:24 [debug] 44231#0: *45709 delete posted event 0000000001448EE0
2016/10/11 11:19:24 [debug] 44231#0: *45709 http wait request handler
2016/10/11 11:19:24 [debug] 44231#0: *45709 malloc: 00000000014E16E0:1024
2016/10/11 11:19:24 [debug] 44231#0: *45709 SSL_read: 144
2016/10/11 11:19:24 [debug] 44231#0: *45709 SSL_read: 6
2016/10/11 11:19:24 [debug] 44231#0: *45709 SSL_read: 149
2016/10/11 11:19:24 [debug] 44231#0: *45709 SSL_read: 7
2016/10/11 11:19:24 [debug] 44231#0: *45709 SSL_read: -1
2016/10/11 11:19:24 [debug] 44231#0: *45709 SSL_get_error: 2
2016/10/11 11:19:24 [debug] 44231#0: *45709 reusable connection: 0
2016/10/11 11:19:24 [debug] 44231#0: *45709 posix_memalign: 00000000015541A0:4096 @16
2016/10/11 11:19:24 [debug] 44231#0: *45709 http process request line
2016/10/11 11:19:24 [debug] 44231#0: *45709 http request line: "POST / HTTP/1.1"
2016/10/11 11:19:24 [debug] 44231#0: *45709 http uri: "/"
2016/10/11 11:19:24 [debug] 44231#0: *45709 http args: ""
2016/10/11 11:19:24 [debug] 44231#0: *45709 http exten: ""
2016/10/11 11:19:24 [debug] 44231#0: *45709 http process request header line
2016/10/11 11:19:24 [debug] 44231#0: *45709 http header: "Host:"
2016/10/11 11:19:24 [debug] 44231#0: *45709 http header: "Transfer-Encoding: chunked"
2016/10/11 11:19:24 [debug] 44231#0: *45709 http header: "Accept: */*"
2016/10/11 11:19:24 [debug] 44231#0: *45709 http header: "content-type: application/json"
2016/10/11 11:19:24 [debug] 44231#0: *45709 posix_memalign: 0000000001466290:4096 @16
2016/10/11 11:19:24 [debug] 44231#0: *45709 http header: "Content-Length: 149"
2016/10/11 11:19:24 [debug] 44231#0: *45709 http header done
2016/10/11 11:19:24 [debug] 44231#0: *45709 event timer del: 8: 1476177622411
2016/10/11 11:19:24 [debug] 44231#0: *45709 generic phase: 0
2016/10/11 11:19:24 [debug] 44231#0: *45709 rewrite phase: 1
2016/10/11 11:19:24 [debug] 44231#0: *45709 test location: "/"
2016/10/11 11:19:24 [debug] 44231#0: *45709 using configuration "/"
2016/10/11 11:19:24 [debug] 44231#0: *45709 http cl:-1 max:1048576
2016/10/11 11:19:24 [debug] 44231#0: *45709 rewrite phase: 3
2016/10/11 11:19:24 [debug] 44231#0: *45709 http set discard body
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 39 s:0
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 35 s:1
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 0D s:1
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 0A s:3
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 7B s:4
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 0D s:5
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 0A s:6
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 30 s:0
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 0D s:1
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 0A s:8
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 0D s:9
2016/10/11 11:19:24 [debug] 44231#0: *45709 http chunked byte: 0A s:10
2016/10/11 11:19:24 [debug] 44231#0: *45709 xslt filter header
2016/10/11 11:19:24 [debug] 44231#0: *45709 HTTP/1.1 200 OK
2016/10/11 11:19:24 [debug] 44231#0: *45709 write new buf t:1 f:0 00000000014664B0, pos 00000000014664B0, size: 160 file: 0, size: 0
2016/10/11 11:19:24 [debug] 44231#0: *45709 http write filter: l:0 f:0 s:160
2016/10/11 11:19:24 [debug] 44231#0: *45709 http output filter "/?"
2016/10/11 11:19:24 [debug] 44231#0: *45709 http copy filter: "/?"
2016/10/11 11:19:24 [debug] 44231#0: *45709 image filter
2016/10/11 11:19:24 [debug] 44231#0: *45709 xslt filter body
2016/10/11 11:19:24 [debug] 44231#0: *45709 http postpone filter "/?" 00007FFFADE3C420
2016/10/11 11:19:24 [debug] 44231#0: *45709 write old buf t:1 f:0 00000000014664B0, pos 00000000014664B0, size: 160 file: 0, size: 0
2016/10/11 11:19:24 [debug] 44231#0: *45709 write new buf t:0 f:0 0000000000000000, pos 0000000000000000, size: 0 file: 0, size: 0
2016/10/11 11:19:24 [debug] 44231#0: *45709 http write filter: l:1 f:0 s:160
2016/10/11 11:19:24 [debug] 44231#0: *45709 http write filter limit 0
2016/10/11 11:19:24 [debug] 44231#0: *45709 posix_memalign: 00000000014672A0:256 @16
2016/10/11 11:19:24 [debug] 44231#0: *45709 malloc: 000000000151CF30:16384
2016/10/11 11:19:24 [debug] 44231#0: *45709 SSL buf copy: 160
2016/10/11 11:19:24 [debug] 44231#0: *45709 SSL to write: 160
2016/10/11 11:19:24 [debug] 44231#0: *45709 SSL_write: 160
2016/10/11 11:19:24 [debug] 44231#0: *45709 http write filter 0000000000000000
2016/10/11 11:19:24 [debug] 44231#0: *45709 http copy filter: 0 "/?"
2016/10/11 11:19:24 [debug] 44231#0: *45709 http finalize request: 0, "/?" a:1, c:1
2016/10/11 11:19:24 [debug] 44231#0: *45709 set http keepalive handler
2016/10/11 11:19:24 [debug] 44231#0: *45709 http close request
2016/10/11 11:19:24 [debug] 44231#0: *45709 http log handler
2016/10/11 11:19:24 [debug] 44231#0: *45709 free: 00000000015541A0, unused: 1
2016/10/11 11:19:24 [debug] 44231#0: *45709 free: 0000000001466290, unused: 3110
2016/10/11 11:19:24 [debug] 44231#0: *45709 free: 00000000014E16E0
2016/10/11 11:19:24 [debug] 44231#0: *45709 hc free: 0000000000000000 0
2016/10/11 11:19:24 [debug] 44231#0: *45709 hc busy: 0000000000000000 0
2016/10/11 11:19:24 [debug] 44231#0: *45709 free: 000000000151CF30
2016/10/11 11:19:24 [debug] 44231#0: *45709 tcp_nodelay
2016/10/11 11:19:24 [debug] 44231#0: *45709 reusable connection: 1
2016/10/11 11:19:24 [debug] 44231#0: *45709 event timer add: 8: 65000:1476177629112
2016/10/11 11:20:29 [debug] 44231#0: *45709 event timer del: 8: 1476177629112
2016/10/11 11:20:29 [debug] 44231#0: *45709 http keepalive handler
2016/10/11 11:20:29 [debug] 44231#0: *45709 close http connection: 8
2016/10/11 11:20:29 [debug] 44231#0: *45709 SSL_shutdown: 1
2016/10/11 11:20:29 [debug] 44231#0: *45709 reusable connection: 0
2016/10/11 11:20:29 [debug] 44231#0: *45709 free: 0000000000000000
2016/10/11 11:20:29 [debug] 44231#0: *45709 free: 0000000000000000
2016/10/11 11:20:29 [debug] 44231#0: *45709 free: 00000000014EA310, unused: 8
2016/10/11 11:20:29 [debug] 44231#0: *45709 free: 00000000014E9EA0, unused: 8
2016/10/11 11:20:29 [debug] 44231#0: *45709 free: 00000000014672A0, unused: 144
2016/10/11 12:20:38 [debug] 44231#0: *46332 event timer add: 4: 60000:1476181298580
2016/10/11 12:20:38 [debug] 44231#0: *46332 reusable connection: 1
2016/10/11 12:20:38 [debug] 44231#0: *46332 epoll add event: fd:4 op:1 ev:80002001
2016/10/11 12:20:39 [debug] 44231#0: *46332 post event 0000000001449120
2016/10/11 12:20:39 [debug] 44231#0: *46332 delete posted event 0000000001449120
2016/10/11 12:20:39 [debug] 44231#0: *46332 http check ssl handshake
2016/10/11 12:20:39 [debug] 44231#0: *46332 http recv(): 1
2016/10/11 12:20:39 [debug] 44231#0: *46332 https ssl handshake: 0x16
2016/10/11 12:20:39 [debug] 44231#0: *46332 SSL_do_handshake: -1
2016/10/11 12:20:39 [debug] 44231#0: *46332 SSL_get_error: 2
2016/10/11 12:20:39 [debug] 44231#0: *46332 reusable connection: 0
2016/10/11 12:20:39 [debug] 44231#0: *46332 post event 0000000001449120
2016/10/11 12:20:39 [debug] 44231#0: *46332 delete posted event 0000000001449120
2016/10/11 12:20:39 [debug] 44231#0: *46332 SSL handshake handler: 0
2016/10/11 12:20:39 [debug] 44231#0: *46332 SSL_do_handshake: -1
2016/10/11 12:20:39 [debug] 44231#0: *46332 SSL_get_error: 2
2016/10/11 12:20:58 [debug] 44231#0: *46332 post event 0000000001449120
2016/10/11 12:20:58 [debug] 44231#0: *46332 delete posted event 0000000001449120
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL handshake handler: 0
2016/10/11 12:20:58 [debug] 44231#0: *46332 verify:1, error:0, depth:1, subject¡
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL_do_handshake: -1
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL_get_error: 2
2016/10/11 12:20:58 [debug] 44231#0: *46332 post event 0000000001449120
2016/10/11 12:20:58 [debug] 44231#0: *46332 delete posted event 0000000001449120
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL handshake handler: 0
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL_do_handshake: -1
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL_get_error: 2
2016/10/11 12:20:58 [debug] 44231#0: *46332 post event 0000000001449120
2016/10/11 12:20:58 [debug] 44231#0: *46332 delete posted event 0000000001449120
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL handshake handler: 0
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL_do_handshake: 1
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL: TLSv1.2, cipher: 
2016/10/11 12:20:58 [debug] 44231#0: *46332 reusable connection: 1
2016/10/11 12:20:58 [debug] 44231#0: *46332 http wait request handler
2016/10/11 12:20:58 [debug] 44231#0: *46332 malloc: 00000000014456D0:1024
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL_read: -1
2016/10/11 12:20:58 [debug] 44231#0: *46332 SSL_get_error: 2
2016/10/11 12:20:58 [debug] 44231#0: *46332 free: 00000000014456D0
2016/10/11 12:20:59 [debug] 44231#0: *46332 post event 0000000001449120
2016/10/11 12:20:59 [debug] 44231#0: *46332 delete posted event 0000000001449120
2016/10/11 12:20:59 [debug] 44231#0: *46332 http wait request handler
2016/10/11 12:20:59 [debug] 44231#0: *46332 malloc: 00000000014456D0:1024
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL_read: 144
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL_read: -1
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL_get_error: 2
2016/10/11 12:20:59 [debug] 44231#0: *46332 reusable connection: 0
2016/10/11 12:20:59 [debug] 44231#0: *46332 posix_memalign: 00000000016F1CC0:4096 @16
2016/10/11 12:20:59 [debug] 44231#0: *46332 http process request line
2016/10/11 12:20:59 [debug] 44231#0: *46332 http request line: "POST / HTTP/1.1"
2016/10/11 12:20:59 [debug] 44231#0: *46332 http uri: "/"
2016/10/11 12:20:59 [debug] 44231#0: *46332 http args: ""
2016/10/11 12:20:59 [debug] 44231#0: *46332 http exten: ""
2016/10/11 12:20:59 [debug] 44231#0: *46332 http process request header line
2016/10/11 12:20:59 [debug] 44231#0: *46332 http header: "Host:"
2016/10/11 12:20:59 [debug] 44231#0: *46332 http header: "Transfer-Encoding: chunked"
2016/10/11 12:20:59 [debug] 44231#0: *46332 http header: "Accept: */*"
2016/10/11 12:20:59 [debug] 44231#0: *46332 http header: "content-type: application/json"
2016/10/11 12:20:59 [debug] 44231#0: *46332 posix_memalign: 00000000014974A0:4096 @16
2016/10/11 12:20:59 [debug] 44231#0: *46332 http header: "Content-Length: 149"
2016/10/11 12:20:59 [debug] 44231#0: *46332 post event 0000000001449120
2016/10/11 12:20:59 [debug] 44231#0: *46332 delete posted event 0000000001449120
2016/10/11 12:20:59 [debug] 44231#0: *46332 http process request header line
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL_read: 6
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL_read: 149
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL_read: 7
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL_read: -1
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL_get_error: 2
2016/10/11 12:20:59 [debug] 44231#0: *46332 http header done
2016/10/11 12:20:59 [debug] 44231#0: *46332 event timer del: 4: 1476181298580
2016/10/11 12:20:59 [debug] 44231#0: *46332 generic phase: 0
2016/10/11 12:20:59 [debug] 44231#0: *46332 rewrite phase: 1
2016/10/11 12:20:59 [debug] 44231#0: *46332 test location: "/"
2016/10/11 12:20:59 [debug] 44231#0: *46332 using configuration "/"
2016/10/11 12:20:59 [debug] 44231#0: *46332 http cl:-1 max:1048576
2016/10/11 12:20:59 [debug] 44231#0: *46332 rewrite phase: 3
2016/10/11 12:20:59 [debug] 44231#0: *46332 http set discard body
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 39 s:0
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 35 s:1
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 0D s:1
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 0A s:3
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 7B s:4
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 0D s:5
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 0A s:6
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 30 s:0
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 0D s:1
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 0A s:8
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 0D s:9
2016/10/11 12:20:59 [debug] 44231#0: *46332 http chunked byte: 0A s:10
2016/10/11 12:20:59 [debug] 44231#0: *46332 xslt filter header
2016/10/11 12:20:59 [debug] 44231#0: *46332 HTTP/1.1 200 OK
2016/10/11 12:20:59 [debug] 44231#0: *46332 write new buf t:1 f:0 00000000014976C0, pos 00000000014976C0, size: 160 file: 0, size: 0
2016/10/11 12:20:59 [debug] 44231#0: *46332 http write filter: l:0 f:0 s:160
2016/10/11 12:20:59 [debug] 44231#0: *46332 http output filter "/?"
2016/10/11 12:20:59 [debug] 44231#0: *46332 http copy filter: "/?"
2016/10/11 12:20:59 [debug] 44231#0: *46332 image filter
2016/10/11 12:20:59 [debug] 44231#0: *46332 xslt filter body
2016/10/11 12:20:59 [debug] 44231#0: *46332 http postpone filter "/?" 00007FFFADE3C4A0
2016/10/11 12:20:59 [debug] 44231#0: *46332 write old buf t:1 f:0 00000000014976C0, pos 00000000014976C0, size: 160 file: 0, size: 0
2016/10/11 12:20:59 [debug] 44231#0: *46332 write new buf t:0 f:0 0000000000000000, pos 0000000000000000, size: 0 file: 0, size: 0
2016/10/11 12:20:59 [debug] 44231#0: *46332 http write filter: l:1 f:0 s:160
2016/10/11 12:20:59 [debug] 44231#0: *46332 http write filter limit 0
2016/10/11 12:20:59 [debug] 44231#0: *46332 posix_memalign: 00000000014E78E0:256 @16
2016/10/11 12:20:59 [debug] 44231#0: *46332 malloc: 000000000147DF50:16384
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL buf copy: 160
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL to write: 160
2016/10/11 12:20:59 [debug] 44231#0: *46332 SSL_write: 160
2016/10/11 12:20:59 [debug] 44231#0: *46332 http write filter 0000000000000000
2016/10/11 12:20:59 [debug] 44231#0: *46332 http copy filter: 0 "/?"
2016/10/11 12:20:59 [debug] 44231#0: *46332 http finalize request: 0, "/?" a:1, c:1
2016/10/11 12:20:59 [debug] 44231#0: *46332 set http keepalive handler
2016/10/11 12:20:59 [debug] 44231#0: *46332 http close request
2016/10/11 12:20:59 [debug] 44231#0: *46332 http log handler
2016/10/11 12:20:59 [debug] 44231#0: *46332 free: 00000000016F1CC0, unused: 1
2016/10/11 12:20:59 [debug] 44231#0: *46332 free: 00000000014974A0, unused: 3108
2016/10/11 12:20:59 [debug] 44231#0: *46332 free: 00000000014456D0
2016/10/11 12:20:59 [debug] 44231#0: *46332 hc free: 0000000000000000 0
2016/10/11 12:20:59 [debug] 44231#0: *46332 hc busy: 0000000000000000 0
2016/10/11 12:20:59 [debug] 44231#0: *46332 free: 000000000147DF50
2016/10/11 12:20:59 [debug] 44231#0: *46332 tcp_nodelay
2016/10/11 12:20:59 [debug] 44231#0: *46332 reusable connection: 1
2016/10/11 12:20:59 [debug] 44231#0: *46332 event timer add: 4: 65000:1476181324667
2016/10/11 12:22:04 [debug] 44231#0: *46332 event timer del: 4: 1476181324667
2016/10/11 12:22:04 [debug] 44231#0: *46332 http keepalive handler
2016/10/11 12:22:04 [debug] 44231#0: *46332 close http connection: 4
2016/10/11 12:22:04 [debug] 44231#0: *46332 SSL_shutdown: 1
2016/10/11 12:22:04 [debug] 44231#0: *46332 reusable connection: 0
2016/10/11 12:22:04 [debug] 44231#0: *46332 free: 0000000000000000
2016/10/11 12:22:04 [debug] 44231#0: *46332 free: 0000000000000000
2016/10/11 12:22:04 [debug] 44231#0: *46332 free: 00000000015D79A0, unused: 8
2016/10/11 12:22:04 [debug] 44231#0: *46332 free: 000000000156C5F0, unused: 8
2016/10/11 12:22:04 [debug] 44231#0: *46332 free: 00000000014E78E0, unused: 144

这是我的代码:

#!/usr/bin/env python
import re

file = open('log_to_parse.txt', 'r')
openFile = file.readlines()
file.close()
resultsFile = open('resultsFile.txt', 'a')
printList = []
identifierNew ="45"
identifierOld = "467"
insideReused = False
#reuseSession = []
sentencesToFind = ["http check ssl handshake","SSL: TLSv1.2, cipher:","http process request line","http close request","SSL reused session"]
for line in openFile:
    lineSplitted = line.split(' ')
    #print lineSplitted[0], lineSplitted[3], lineSplitted[1]
    identifierOld = lineSplitted[3]
    print lineSplitted[3]
    for phrase in sentencesToFind:
        if phrase in line:
            #if identifierNew != identifierOld:
            #   print >> resultsFile, "\n"
            #   printList = []
            if sentencesToFind.index(phrase) == 0:
                #print sentencesToFind.index(phrase)
                printList.append(lineSplitted[3]) #+ " " + lineSplitted[0] + " " + lineSplitted[1] + " ")
                printList.append(lineSplitted[0])
                printList.append(lineSplitted[1])
            elif sentencesToFind.index(phrase) == 4:
                printList.append("1")
                insideReused = True
            else:
                printList.append(lineSplitted[1])
                if sentencesToFind.index(phrase) == 4 and not insideReused:
                    printList.append("0")
    identifierNew = identifierOld
    insideReused = False
    if printList: #and identifierOld != identifierNew:
        #print >> resultsFile, "\n"
        print >>resultsFile, printList
        printList = []
resultsFile.close()

一些想法?谢谢:))

2 个答案:

答案 0 :(得分:1)

要保存文件,使用numpy更容易。原始数组在包含'#'

的每个元素处拆分
import numpy as np
import re

file = open('log_to_parse.txt', 'r')
openFile = file.readlines()
file.close()
#resultsFile = open('resultsFile.txt', 'a')
printList = []
identifierNew ="45"
identifierOld = "467"
insideReused = False
#reuseSession = []
sentencesToFind = ["http check ssl handshake",
                   "SSL: TLSv1.2, cipher:",
                   "http process request line",
                   "http close request",
                   "SSL reused session"]
for line in openFile:
    lineSplitted = line.split(' ')
    #print lineSplitted[0], lineSplitted[3], lineSplitted[1]
    identifierOld = lineSplitted[3]
#     print(lineSplitted[3])
    for phrase in sentencesToFind:
        if phrase in line:
            #if identifierNew != identifierOld:
            #   print >> resultsFile, "\n"
            #   printList = []
            if sentencesToFind.index(phrase) == 0:
                #print sentencesToFind.index(phrase)
                printList.append(lineSplitted[3]) #+ " " + lineSplitted[0] + " " + lineSplitted[1] + " ")
                printList.append(lineSplitted[0])
                printList.append(lineSplitted[1])
            elif sentencesToFind.index(phrase) == 4:
                printList.append("1")
                insideReused = True
            else:
                printList.append(lineSplitted[1])
                if sentencesToFind.index(phrase) == 4 and not insideReused:
                    printList.append("0")
    identifierNew = identifierOld
    insideReused = False

# I did not see the need to put the for loop where you write the content inside the previous for loop
split_list = []
start_sub_list=0
for i in range(1,len(printList)):
    if '#' in printList[i]:
        temp_list = printList[start_sub_list:i]
        split_list.append(temp_list)
        start_sub_list=i

#The last element will always be left out. So,
temp_list = printList[start_sub_list:i]
split_list.append(temp_list)

split_list = np.array(split_list)
print(split_list)
np.savetxt('resultsFile.txt', split_list, fmt="%s")

print(split_list)产生以下作为输出,这将写入文件

[['44229#0:', '2016/10/11', '11:15:57', '11:15:57', '11:15:57', '11:15:58']
 ['44231#0:', '2016/10/11', '11:19:23', '11:19:23', '1', '11:19:24', '11:19:24']
 ['44231#0:', '2016/10/11', '12:20:39', '12:20:58', '12:20:59']]

答案 1 :(得分:0)

if printList块是否应该缩进?

或者至少不将每个行的列表设置为[]。