2013-06-13 59 views
2

我想讀取文本文件並以特定方式解析它,然後用輸出重寫該文件。解析文本文件並將某些值分組

該文本文件(輸入)看起來像這樣:

2 108 1 561 1 20 28 1 
2 108 2 557 1 24 32 1 
5 28 1 553 197 20 20 1 
5 28 2 552 197 23 21 1 
6 23 1 113 393 36 36 1 
6 23 2 113 391 39 39 1 

每一列代表一個特定的值作爲這樣:

[ID] [Length] [Frame] [X] [Y] [W] [H] 

因此,對於一個實例,這一行:

2 108 1 561 1 20 28 1 

is actualy:ID:2, Length:108, Frame:1, X:561, Y:1, W:20, Y:28

最後的值1根本不需要。

現在這裏的是我怎麼做,到目前爲止:

with open('1.txt') as fin: 
    frame_rects = {} 
    for row in (map(int, line.split()) for line in fin): 
     id, frame, rect = row[0], row[2], row[3:7] 
     frame_rects[frame] = (id, rect) 
     first_data = ('{} {} {}\n'.format(frame, id, rect)) 
     print first_data 

而這種輸出以下:

1 2 [561, 1, 20, 28] 

2 2 [557, 1, 24, 32] 

1 5 [553, 197, 20, 20] 

2 5 [552, 197, 23, 21] 

1 6 [113, 393, 36, 36] 

2 6 [113, 391, 39, 39] 

這是第一步,但我預期的輸出結果如下:

1 2 [561, 1, 20, 28] 5 [553, 197, 20, 20] 6 [113, 393, 36, 36] 
2 2 [557, 1, 24, 32] 5 [552, 197, 23, 21] 6 [113, 391, 39, 39] 

因此,對於每一幀,我附加所有的ID和他們的值出現在特定的fram即

因此在第1幀中,id 2,5和6分別出現了各自的值(x,y,w,h)。

每一幀密鑰都是唯一的,但只要它們實際出現在該幀中,就可以根據需要保存多個ID +值。

我需要在可能包含數千個文件的文本文件上運行此操作。每幀可以容納20個不同的ID。我將如何能夠實現預期的產出?

回答

2

這樣做:

from collections import defaultdict 

with open('1.txt') as fin: 
    frame_rects = defaultdict(list) 
    for row in (map(int, line.split()) for line in fin): 
     id, frame, rect = row[0], row[2], row[3:7] 
     frame_rects[frame].append((id, rect)) 
     # print '{} {} {}'.format(frame, id, rect) # (if you want to sample) 

for key, value in frame_rects.items(): 
    print key, ' '.join([' '.join([str(i) for i in v]) for v in value]) 

輸出:

1 2 [561, 1, 20, 28] 5 [553, 197, 20, 20] 6 [113, 393, 36, 36] 
2 2 [557, 1, 24, 32] 5 [552, 197, 23, 21] 6 [113, 391, 39, 39] 
3
from collections import defaultdict 
with open('abc') as f: 
    dic = defaultdict(list) 
    for line in f: 
     idx, lenght, frame, X, Y, W, H, _ = map(int, line.split()) 
     dic[frame].append([idx, [X, Y, W, H] ]) 
print dic 
print "Expected output:" 
for k, v in dic.items(): 
    print "{} {}".format(k, "".join(["{} {} ".format(*lis) for lis in v ]) ) 

輸出:

defaultdict(<type 'list'>, 
{1: [[2, [561, 1, 20, 28]], [5, [553, 197, 20, 20]], [6, [113, 393, 36, 36]]], 
2: [[2, [557, 1, 24, 32]], [5, [552, 197, 23, 21]], [6, [113, 391, 39, 39]]]}) 
Expected output: 
1 2 [561, 1, 20, 28] 5 [553, 197, 20, 20] 6 [113, 393, 36, 36] 
2 2 [557, 1, 24, 32] 5 [552, 197, 23, 21] 6 [113, 391, 39, 39] 
相關問題