2017-02-03 5 views
0

これが適切な場所でない場合は、私に許してください。いくつかのテキストを分割します。ここで ' n'と ' n n'の両方で終わる分割線

は私が分割しようとしていたテキストのサンプルです:

[Thu Feb 2 12:45:38 2017][428423.3] (file_name:0xcb61) Invalid variable type 
call stack: 
----------- 
[0cb61:+33] larray, r#26, fp(3), 
[031ff:Mug::Request.preHandlers+17] refcall, fp(1), string#245, # from: fp(1) 
[0339d:Mug::Request.process+77] call, addr(0x80001d), -, # Mug::Request.preHandlers() 
[02ffd:Mug::Request.recv+93] call, addr(0x800026), -, # Mug::Request.process() 
[02d03:Mug::Connection.on_client+101] refcall, fp(0), string#734, # from: fp(0) 
[14a5b:+4] refcall, fp(-2), string#3103, # from: fp(-2) 
[1e24a:main+9664] eop, -, -, 

[Thu Feb 2 14:09:07 2017][428423.8] Warning: writing 0 byte file (/the_directory/) to tar archive 
[Thu Feb 2 18:55:27 2017][449547.25] Warning: writing 0 byte file (/the_directory/) to tar archive 
[Fri Feb 3 12:21:33 2017][451135.3] (file_name:0xcb61) Invalid variable type 
call stack: 
----------- 
[0cb61:+33] larray, r#26, fp(3), 
[031ff:Mug::Request.preHandlers+17] refcall, fp(1), string#245, # from: fp(1) 
[0339d:Mug::Request.process+77] call, addr(0x80001d), -, # Mug::Request.preHandlers() 
[02ffd:Mug::Request.recv+93] call, addr(0x800026), -, # Mug::Request.process() 
[02d03:Mug::Connection.on_client+101] refcall, fp(0), string#734, # from: fp(0) 
[14a5b:+4] refcall, fp(-2), string#3103, # from: fp(-2) 
[1e24a:main+9664] eop, -, -, 

あなたは上記のテキストは、実際のパターンの任意の並べ替えを適合しない、上記の見ることができるように、と多少の誤差があること空白の改行とそれ以外のものを投げる。理想的には私がで終わるたいのは、このような何か...

[[Thu Feb 2 14:09:07 2017][428423.8] Warning: writing 0 byte file (/the_directory/) to tar archive], [Thu Feb 2 12:45:38 2017][428423.3] (file_name:0xcb61) Invalid variable type \ncall stack:\n-----------\n[0cb61:+33] larray, r#26, fp(3),\n[031ff:Mug::Request.preHandlers+17] refcall, fp(1), string#245, # from: fp(1)\n[0339d:Mug::Request.process+77] call, addr(0x80001d), -, # Mug::Request.preHandlers()\n[02ffd:Mug::Request.recv+93] call, addr(0x800026), -, # Mug::Request.process()\n[02d03:Mug::Connection.on_client+101] refcall, fp(0), string#734, # from: fp(0)\n[14a5b:+4] refcall, fp(-2), string#3103, # from: fp(-2)\n[1e24a:main+9664] eop, -, -,] 

され、その後、私はループを介して、各エラーにアクセスすることができました。今私はいくつかの正規表現を使用して、この既知の良いデータをフィルタリングして呼び出しスタックを投げ捨てることに近づいていますが、可能な限り呼び出しスタック全体を格納できるようにしたいと考えています。ここで

は私の現在のコードです:

with open(local_dump, 'r') as ifile: 
    for line in ifile: 
     filename_pattern = re.compile(r'\((\w*\.\w*)\:\w*\)\s(.*$)') 
     date_pattern = re.compile(r"^\[([a-zA-z]{3,})\s([a-zA-z]{3,})\s{2}(\d{1,2})\s(\d{1,2}\:\d{1,2}\:\d{1,2})\s(\d{4})\]\[\d*\.\d*\]\s(.*$)") 
     if re.search(date_pattern, line): 
      data = re.search(date_pattern, line) 
      if re.search(filename_pattern, (data[6])): 
       data = re.search(filename_pattern, (data[6])) 
       print("{0}: {1}".format(data.group(1),data.group(2))) 
     else: 
      if re.search("call stack", line.strip()): 
       print(line.strip()) 

私はこのコードブロックで、これはほとんどの機能を得ることができました:

with open(local_dump, 'r') as ifile: 
     lines = ifile.read() 
     for line in lines.split('\n\n'): 
      print("LINE: " + line) 

上記のコードは、独自にコールスタックを抜け出すました行が '\ n'で終わると問題に遭遇しました。

LINE: [Thu Feb 2 12:45:38 2017][428423.3] (file_name:0xcb61) Invalid variable type 
call stack: 
----------- 
[0cb61:+33] larray, r#26, fp(3), 
[031ff:Mug::Request.preHandlers+17] refcall, fp(1), string#245, # from: fp(1) 
[0339d:Mug::Request.process+77] call, addr(0x80001d), -, # Mug::Request.preHandlers() 
[02ffd:Mug::Request.recv+93] call, addr(0x800026), -, # Mug::Request.process() 
[02d03:Mug::Connection.on_client+101] refcall, fp(0), string#734, # from: fp(0) 
[14a5b:+4] refcall, fp(-2), string#3103, # from: fp(-2) 
[1e24a:main+9664] eop, -, -, 
LINE: [Thu Feb 2 14:09:07 2017][428423.8] Warning: writing 0 byte file (/the_directory/) to tar archive 
[Thu Feb 2 18:55:27 2017][449547.25] Warning: writing 0 byte file (/the_directory/) to tar archive 
[Fri Feb 3 12:21:33 2017][451135.3] (file_name:0xcb61) Invalid variable type 
call stack: 
----------- 
[0cb61:+33] larray, r#26, fp(3), 
[031ff:Mug::Request.preHandlers+17] refcall, fp(1), string#245, # from: fp(1) 
[0339d:Mug::Request.process+77] call, addr(0x80001d), -, # Mug::Request.preHandlers() 
[02ffd:Mug::Request.recv+93] call, addr(0x800026), -, # Mug::Request.process() 
[02d03:Mug::Connection.on_client+101] refcall, fp(0), string#734, # from: fp(0) 
[14a5b:+4] refcall, fp(-2), string#3103, # from: fp(-2) 
[1e24a:main+9664] eop, -, -, 

extは、より生の形式になります。

'[Thu Feb 2 14:09:07 2017][428423.8] Warning: writing 0 byte file (/the_directory/) to tar archive \n[Thu Feb 2 18:55:27 2017][449547.25] Warning: writing 0 byte file (/the_directory/) to tar archive \n[Fri Feb 3 12:21:33 2017][451135.3] (file_name:0xcb61) Invalid variable type \ncall stack:\n-----------\n[0cb61:+33] larray, r#26, fp(3), \n[031ff:Mug::Request.preHandlers+17] refcall, fp(1), string#245, # from: fp(1)\n[0339d:Mug::Request.process+77] call, addr(0x80001d), -, # Mug::Request.preHandlers()\n[02ffd:Mug::Request.recv+93] call, addr(0x800026), -, # Mug::Request.process()\n[02d03:Mug::Connection.on_client+101] refcall, fp(0), string#734, # from: fp(0)\n[14a5b:+4] refcall, fp(-2), sting#3103, # from: fp(-2)\n[1e24a:main+9664] eop, -, -, ' 

任意のヒントのおかげで、トリック、そしてあなたが提供することができます助けを。

+0

が先行していると仮定すると、あなたは「( 'lines.splitを試してみました\ n ') '? – Peter

+0

@PeterKuebler - 私は、申し訳ありませんが、それを書いて私の急いでそれを残している。その問題は、コールスタック全体で '\ n'が散らばっていることです。これは少しばかり混乱しています。 –

+0

ああ、私はあなたがそれらを保持したいとは思わなかった...その 'for'ループの代わりに' lines.replace( '\\'、 '\\\\');を呼び出すことによって\をエスケープしようとしました。 forループなしで 'print(lines)'を実行します。 – Peter

答えて

2

\nに分割して空白行を削除することができます。

input = "your input" 
list = input.split("\n") 
list = filter(None, list) 

あなたはちょうどあなたが試すことができますログからすべてのエラーメッセージを取得したい場合:

matches = re.finditer(r"\[.*?\]\[.*\]\s*(.*)$", input, re.MULTILINE) 
for match in matches: 
    print("Error: " + match.group(1)) 

すべてのエラーが2つの[...]グループ

+0

これは私が "呼び出しスタック"エラーで欲しかったものを私にはっきりと渡しませんでしたが、これは素晴らしい応答であり、私を大いに助けてくれます。答えとしてマークして、ありがとう! –

関連する問題