2016-03-08 135 views
2

我有一個文本文件,表示來自視頻剪輯的運動矢量數據。從Python中的文本文件中提取數據

# pts=-26 frame_index=2 pict_type=P output_type=raw shape=3067x4 
8 8 0 0 
24 8 0 -1 
40 8 0 0 
... 
8 24 0 0 
24 24 3 1 
40 24 0 0 
... 
8 40 0 0 
24 40 0 0 
40 40 0 0 
# pts=-26 frame_index=3 pict_type=P output_type=raw shape=3067x4 
8 8 0 1 
24 8 0 0 
40 8 0 0 
... 
8 24 0 0 
24 24 5 -3 
40 24 0 0 
... 
8 40 0 0 
24 40 0 0 
40 40 0 0 
... 

所以它是某種格子,前兩位是x和y座標,第三和第四是運動矢量的x和y值。

要進一步使用此數據,我需要提取x和y值對,其中至少有一個值與0不同,並將它們組織在列表中。

例如:

(0, -1, 2) 
(3, 1, 2) 
(0, 1, 3) 
(5, 3, 3) 

第三個數字是一個frame_index。

如果有人冷我的計劃如何破解這個任務,我將不勝感激。從我應該開始。

+0

我假設的例子中'(5,3,3)'應該是'(5,-3,3)'? –

+0

是的。該文件很大,所以我寫了一個小例子來解釋文件中的內容。 –

回答

1

這實際上很簡單,因爲只有一種類型的數據。 我們可以做到這一點,而不訴諸於例如常用表達。

忽略任何錯誤校驗(難道我們真的看3067點幀2,或僅3065?的格式不正確?...行)會是這個樣子

frame_data = {} # maps frame_idx -> list of (x, y, vx, vy) 
for line in open('mydatafile.txt', 'r'): 
    if line.startswith('#'): # a header line 
     options = {key: value for key, value in 
         [token.split('=') for token in line[1:].split()] 
        } 
     curr_frame = int(options['frame_index']) 
     curr_data = [] 
     frame_data[curr_frame] = curr_data 
    else: # Not a header line 
     x, y, vx, vy = map(int, line.split()) 
     frame_data.append((x, y, vx, vy)) 

你知道有一本字典它將一個幀號映射到一個元組元素列表(x, y, vx, vy)

提取從詞典中的新名單現在很容易:

result = [] 
for frame_number, data in frame_data.items(): 
    for x, y, vx, vy in data: 
     if not (vx == 0 and vy == 0): 
      result.append((vx, vy, frame_number)) 
+0

非常感謝!我收到一個AttributeError:'dict'對象沒有屬性'append'。 所以改變 'frame_data.append((X,Y,VX,VY))' 到 'frame_data [curr_frame] .append((X,Y,VX,VY))' –