-2

私が取り組んでいるこの問題について、正しい方向に向ける必要があります。

次のようにCプログラムからの出力を読んでいるとしましょう:

while True:
    ln = p.stdout.readline()
    if '' == ln:
        break
    #do stuff here with ln

そして、私の出力は次の行のようになります。

TrnIq: Thread on CPU 37
TrnIq: Thread on CPU 37 but will be moved to CPU 44
IP-Thread on CPU 33
FANOUT Thread on CPU 37
Filter-Thread on CPU 38 but will be moved to CPU 51
TRN TMR Test 2 Supervisor Thread on CPU 34
HomographyWarp Traking Thread[0] on CPU 26

「TrnIq: Thread on」と「37」を 2 つの個別の変数としてキャプチャしたい: 出力「TrnIq: Thread on CPU 37」からの文字列と数値。

たとえば、「HomographyWarp Traking Thread[0] on」と「HomographyWarp Traking Thread[0] on CPU 26」から # 「26」をキャプチャするための他の行についても同様です。

唯一の実際の課題は、次のような行の場合です。「CPU 38 のフィルター スレッドが、CPU 51 に移動されます」

Python にはこれを行うためのさまざまな方法があり、どこから始めればよいかわかりません。

前もって感謝します!

4

5 に答える 5

4

以下は、データの1行であると想定して情報のタプルを返す必要がlnあります(CPU値をintに変換することを含むように編集されています)。

match = re.match(r'(.*?)(?: on CPU.*)?(?: (?:on|to) CPU )(.*)', ln).groups()
if match:
    proc, cpu = match.groups()
    cpu = int(cpu)

例:

>>> import re
>>> for ln in lines:
...     print re.match(r'(.*?)(?: on CPU.*)?(?: (?:on|to) CPU )(.*)', ln).groups()
... 
('TrnIq: Thread', '37')
('TrnIq: Thread', '44')
('IP-Thread', '33')
('FANOUT Thread', '37')
('Filter-Thread', '51')
('TRN TMR Test 2 Supervisor Thread', '34')
('HomographyWarp Traking Thread[0]', '26')

説明:

(.*?)          # capture zero or more characters at the start of the string,
               #   as few characters as possible
(?: on CPU.*)? # optionally match ' on CPU' followed by any number of characters,
               #   do not capture this
(?: (?:on|to) CPU )  # match ' on CPU ' or ' to CPU ', but don't capture
(.*)           # capture the rest of the line

Rubular:http ://www.rubular.com/r/HqS9nGdmbM

于 2012-07-20T16:53:03.443 に答える
2

正規表現は、ここではやり過ぎのようです。[免責事項:私は正規表現は好きではありませんが、Pythonは好きなので、可能な場合はPythonで記述し、正規表現は記述しません。理由から、これが驚くべきことだと完全に理解したことはありません。]

s = """TrnIq: Thread on CPU 37
TrnIq: Thread on CPU 37 but will be moved to CPU 44
IP-Thread on CPU 33
FANOUT Thread on CPU 37
Filter-Thread on CPU 38 but will be moved to CPU 51
TRN TMR Test 2 Supervisor Thread on CPU 34
HomographyWarp Traking Thread[0] on CPU 26"""

for line in s.splitlines():
    words = line.split()
    if not ("CPU" in words and "on" in words): continue # skip uninteresting lines
    prefix_words = words[:words.index("on")+1]
    prefix = ' '.join(prefix_words)
    cpu = int(words[-1])
    print (prefix, cpu)

与える

('TrnIq: Thread on', 37)
('TrnIq: Thread on', 44)
('IP-Thread on', 33)
('FANOUT Thread on', 37)
('Filter-Thread on', 51)
('TRN TMR Test 2 Supervisor Thread on', 34)
('HomographyWarp Traking Thread[0] on', 26)

このコードを英語に翻訳する必要はないと思います。

于 2012-07-20T17:08:10.357 に答える
1

したがって、正規表現を使用します^(.*?)\s+on\s+CPU.*(?<=\sCPU)\s+(\d+)\s*$

import sys
import re

for ln in sys.stdin:
  m = re.match(r'^(.*?)\s+on\s+CPU.*(?<=\sCPU)\s+(\d+)\s*$', ln); 
  if m is not None:
    print m.groups();

こちらの例を参照してテストしてください。

于 2012-07-20T16:54:26.920 に答える
1

2 つの正規表現検索で非常に簡単に実行できます。

import re

while True:
    ln = p.stdout.readline()
    if '' == ln:
        break

    start_match = re.search(r'^(.*?) on', ln)
    end_match = re.search(r'(\d+)$', ln)
    process = start_match and start_match.group(0)
    process_number = end_match and end_match.group(0)
于 2012-07-20T17:00:06.293 に答える
1

あなたが言及した場合、常に2番目のCPU番号が必要なので、単一の正規表現で実行できます。

# Test program
import re

lns = [
    "TrnIq: Thread on CPU 37",
    "TrnIq: Thread on CPU 37 but will be moved to CPU 44",
    "IP-Thread on CPU 33",
    "FANOUT Thread on CPU 37",
    "Filter-Thread on CPU 38 but will be moved to CPU 51",
    "TRN TMR Test 2 Supervisor Thread on CPU 34",
    "HomographyWarp Traking Thread[0] on CPU 26"
]

for ln in lns:
    test    = re.search("(?P<process>.*Thread\S* on).* CPU (?P<cpu>\d+)$", ln)
    print "%s: '%s' on CPU #%s" % ( ln, test.group('process'), test.group('cpu'))

一般的なケースでは、ケースを区別したい場合があります (例: CPU 上のスレッド、スレッドの移動、サブスレッドなど)。これを行うには、いくつかの re.search()es を次々に使用できます。例えば:

# This search recognizes lines of the form "...Thread on CPU so-and-so", and
# also lines that add "...but will be moved to CPU some-other-cpu".
test = re.search("(?P<process>.* Thread) on CPU (?P<cpu1>\d+)( but will be moved to CPU (?P<cpu2>\d+))*", ln)
if test:
   # Here we capture Process Thread, both moved and non moved
   if test.group('cpu2'):
       # We have process, cpu1 and cpu2: moved thread
   else:
       # Nonmoved task, we have test.group('process') and cpu1.
else:
   # No match, try some other regexp. For example processes with a thread number
   # between square brackets: "Thread[0]", which are not captured by the regex above.
   test = re.search("(?P<process>.*) Thread[(?P<thread>\d+)] on CPU (?P<cpu1>)", ln)
   if test:
       # Here we have Homography Traking in process, 0 in thread, 26 in cpu1

最適なパフォーマンスを得るには、より頻繁に発生する行のテストを最初に実行することをお勧めします。

于 2012-07-20T16:55:26.670 に答える