正規表現を使って値を必要な抜き出すした後に、pandasを使って CSVに書き出すとよいのではないでしょうか
Python
1import pandas as pd
2import re
3from pprint import pprint
4
5# ファイル読み込み
6with open('data.txt') as f:
7 text = f.read()
8
9# 正規表現を使ってデータを抜き出す
10pattern = r'^bird:\s*(\d*).*left_x:\s*(\d*).*top_y:\s*(\d*).*width:\s*(\d*).*height:\s*(\d*).*$'
11pat = re.compile(pattern, flags=re.MULTILINE)
12data = []
13for match in pat.finditer(text):
14 data.append({'bird':int(match.group(1)),
15 'left_x':int(match.group(2)),
16 'top_y':int(match.group(3)),
17 'width':int(match.group(4)),
18 'height':int(match.group(5))})
19pprint(data)
20#[{'bird': 30, 'height': 81, 'left_x': 877, 'top_y': 426, 'width': 157},
21# {'bird': 27, 'height': 94, 'left_x': 874, 'top_y': 435, 'width': 158},
22# {'bird': 30, 'height': 86, 'left_x': 877, 'top_y': 425, 'width': 153}]
23
24# DataFrame化
25df = pd.DataFrame(data)
26print(df)
27# bird left_x top_y width height
28#0 30 877 426 157 81
29#1 27 874 435 158 94
30#2 30 877 425 153 86
31
32# csvに書き出す
33df.to_csv('output.csv')
【追加】
frame number 集計部を追加したサンプル
Python
1import pandas as pd
2import re
3from pprint import pprint
4
5# ファイル読み込み
6with open('data.txt') as f:
7 text = f.read()
8
9# 正規表現を使ってデータを抜き出す
10pattern = r'^bird:\s*(\d*).*left_x:\s*(\d*).*top_y:\s*(\d*).*width:\s*(\d*).*height:\s*(\d*).*$'
11pat = re.compile(pattern, flags=re.MULTILINE)
12data = []
13for match in pat.finditer(text):
14 data.append({'bird':int(match.group(1)),
15 'left_x':int(match.group(2)),
16 'top_y':int(match.group(3)),
17 'width':int(match.group(4)),
18 'height':int(match.group(5))})
19pprint(data)
20#[{'bird': 30, 'height': 81, 'left_x': 877, 'top_y': 426, 'width': 157},
21# {'bird': 27, 'height': 94, 'left_x': 874, 'top_y': 435, 'width': 158},
22# {'bird': 30, 'height': 86, 'left_x': 877, 'top_y': 425, 'width': 153}]
23
24# DataFrame化
25df = pd.DataFrame(data)
26print(df)
27# bird left_x top_y width height
28#0 30 877 426 157 81
29#1 27 874 435 158 94
30#2 30 877 425 153 86
31
32# 以下を追加
33# 再度ループを回してObjects:行をカウント
34count = 0
35frame_number = []
36for line in text.split():
37 if line.startswith('Objects:'):
38 count = count + 1
39 if line.startswith('bird:'):
40 frame_number.append(count)
41df['frame_number'] = frame_number
42
43print(df)
44# bird left_x top_y width height frame_number
45#0 30 877 426 157 81 2
46#1 27 874 435 158 94 2
47#2 30 877 425 153 86 3
48
49# csvに書き出す
50df.to_csv('output.csv')
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/01/21 03:18
2020/01/21 16:41
2020/01/22 04:31