Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a0b9246

Browse files
データ処理スクリプトの最初版
1 parent 9d63575 commit a0b9246

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed

txtToCsv.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# import json
2+
# 生データのファイル名
3+
FROM_FILE = "testADB_touchMesocket1_log_2020.05.07.txt"
4+
# 処理済ファイル名と列目
5+
TO_FILE = 'result.txt'
6+
VALUE_FROM = 71
7+
VALUE_TO = 79
8+
COLUMNS = [
9+
# {
10+
# 'name': '被験者番号',
11+
# 'bind_name': None,
12+
# 'from': None,
13+
# 'to': None,
14+
# 'value': 1,
15+
# },
16+
{
17+
'name': 'ミリ秒(n)',
18+
'bind_name': 'TIME', # 固定
19+
'from': 1,
20+
'to': 16,
21+
},
22+
{
23+
'name': 'x軸(n)',
24+
'bind_name': 'ABS_MT_POSITION_X',
25+
'from': VALUE_FROM,
26+
'to': VALUE_TO,
27+
},
28+
{
29+
'name': 'y軸(n)',
30+
'bind_name': 'ABS_MT_POSITION_Y',
31+
'from': VALUE_FROM,
32+
'to': VALUE_TO,
33+
},
34+
{
35+
'name': '圧力(n)',
36+
'bind_name': 'ABS_MT_PRESSURE',
37+
'from': VALUE_FROM,
38+
'to': VALUE_TO,
39+
},
40+
# {
41+
# 'name': '面積(n)',
42+
# 'bind_name': 'ABS_MT_TOUCH_MAJOR',
43+
# 'from': VALUE_FROM,
44+
# 'to': VALUE_TO,
45+
# }
46+
]
47+
48+
# 抽出したいデータの設備名
49+
DEVICE_CODE = '/dev/input/event4'
50+
# 生データに行の並び順番
51+
KEYS = ('ABS_MT_TRACKING_ID', 'BTN_TOUCH', 'ABS_MT_POSITION_X', 'ABS_MT_POSITION_Y', 'ABS_MT_PRESSURE', 'SYN_REPORT')
52+
KEY_FROM = 50
53+
KEY_TO = 68
54+
# イベントの項目名
55+
EVENT_NAME = 'BTN_TOUCH'
56+
# イベントの情報
57+
EVENT_VALUE_FROM = 'DOWN'
58+
EVENT_VALUE_TO = 'UP'
59+
# 被験者ID
60+
USER_ID = '1'
61+
62+
63+
def parse_data():
64+
max = 0
65+
with open(FROM_FILE, 'r', encoding='utf-8') as f:
66+
data = f.readlines()
67+
68+
result_data = []
69+
data_json = {'TIME': []}
70+
i = 0
71+
# データ補充
72+
for row in data:
73+
if row.find(DEVICE_CODE) != -1 and row.find('add device') == -1:
74+
data_json['TIME'].append(row[1: 16])
75+
action = row[KEY_FROM:KEY_TO]
76+
# 省略データがある場合
77+
while action.find(KEYS[i]) < 0 and i < len(KEYS):
78+
# 前回のデータを配列に入れる
79+
data_json[KEYS[i]].append(data_json[KEYS[i]][-1])
80+
i += 1
81+
82+
if action.find(KEYS[i]) >= 0:
83+
if KEYS[i] in data_json:
84+
data_json[KEYS[i]].append(row[VALUE_FROM:VALUE_TO])
85+
else:
86+
data_json[KEYS[i]] = [row[VALUE_FROM:VALUE_TO]]
87+
i += 1
88+
89+
if i == len(KEYS):
90+
i = 0
91+
# print(json.dumps(data_json, indent=4))
92+
93+
tmp = 0
94+
event_count = len(data_json[EVENT_NAME])
95+
out_row_data = [USER_ID]
96+
for j in range(event_count):
97+
tmp += 1
98+
for col in COLUMNS:
99+
out_row_data.append(data_json[col['bind_name']][j])
100+
101+
# イベントがUPの場合
102+
# print(out_row_data)
103+
if data_json[EVENT_NAME][j].find(EVENT_VALUE_TO) != -1:
104+
result_data.append(','.join(out_row_data) + '\n')
105+
out_row_data = [USER_ID]
106+
if tmp > max:
107+
max = tmp
108+
109+
tmp = 0
110+
111+
# ヘッダ作成
112+
print(max)
113+
heads = ['被験者番号']
114+
for k in range(max):
115+
for col in COLUMNS:
116+
heads.append(col['name'].replace('n', str(k + 1)))
117+
118+
result_head = ','.join(heads) + '\n'
119+
120+
with open(TO_FILE, 'a', encoding='utf-8') as f:
121+
# ヘッダ
122+
f.writelines(result_head)
123+
f.writelines(result_data)
124+
125+
126+
if __name__ == '__main__':
127+
parse_data()

0 commit comments

Comments
 (0)