-1

file1 のスレッド番号、実行番号、およびテスト番号を解析し、file2 のテスト番号と一致させ、これらの値を両方とも新しいファイルに書き込む必要があります。

最初のファイルの内容は次のとおりです。

com-0 thread-0 [ run-0, test-1201 ]: https://lp1.soma.sf.com/img/metaBar_sprite.png -> 200 OK, 682 bytes
com-0 thread-0 [ run-0, test-1202 ]: https://lp1.soma.sf.com/img/chattersupersprite.png?v=182-4 -> 200 OK, 40172 bytes
com-0 thread-0 [ run-0, test-1203 ]: https://lp1.soma.sf.com/img/chatter/wtdNowGradientbg.png -> 200 OK, 201 bytes
com-0 thread-0 [ run-0, test-1204 ]: https://lp1.soma.sf.com/img/chatter/wtdNowIcon_sprite.png -> 200 OK, 7280 bytes
com-0 thread-0 [ run-0, test-1205 ]: https://lp1.soma.sf/img/sprites/icons24.png -> 200 OK, 20287 bytes
com-0 thread-0 [ run-0, test-1206 ]: https://lp1.soma.sf.com/img/feeds/follow_sprite.png -> 200 OK, 2894 bytes

2 番目のファイルの内容は次のとおりです。

1 Thread, Run, Test num, Start time, Test time, Errors, HTTP response code, EPQ
2 0, 0, 1201, 1370898725367, 154, 0, 200, 2049 
3 0, 0, 1202, 1370898725523, 505, 0, 204, 0
2 0, 0, 1201, 1370898725367, 400, 0, 200, 2049 
2 0, 0, 1201, 1370898725367, 1124, 0, 200, 2049 
3 0, 0, 1202, 1370898725523, 1405, 0, 204, 0

望ましい出力は次のようになります。

thread-0 [ run-0, test-1201 ]: https://lp1.soma.sf.com/img/metaBar_sprite.png = [154, 400, 1124]
thread-0 [ run-0, test-1202 ]: https://lp1.soma.sf.com/img/chattersupersprite.png?v=182-4 = [505, 1405]

助けてください。前もって感謝します。

4

1 に答える 1

1

2 つのログの構造が変わらない場合は...

log1 = [line.replace(',', '').split() for line in open('test1.txt', 'r')][:]
log2 = [line.replace(',', '').split() for line in open('test2.txt', 'r')][1:]
log3 = [] # need for combining.

これにより、ログ ファイルごとにスペースで区切られたリストが生成されます。次に、キーとそれぞれから必要なデータを一致させることが重要です。

# First, get the tests from the second log.
tests = {}
for line in log2:
    test = line[3] # test number
    if test not in tests:
        tests[test] = {'times': []}

    tests[test]['times'].append(line[5]) # test time

次に、テスト番号ごとに最初のログと照合します。

for line in log1:
    test = line[4].split('-')[1] # changes test-#### to ####
    if test in tests:
        tests[test].update({
            'thread': line[1],
            'run':    line[3],
            'url':    line[6],
            'times':  ', '.join(tests[test]['times'])
        })

あとは、テストdictをまとめてログ ファイルに戻すだけです。

for key, test in tests.iteritems():

    line = '{thread} [ {run}, test-{key} ]: {url} = [{times}]\n'
    line = line.format(thread=test['thread'], run=test['run'], key=key,
        url=test['url'], times=test['times'])

    log3.append(line)

with open('log3.txt', 'a') as f:
    f.write(''.join(log3))
于 2013-06-11T06:41:48.993 に答える