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

Skip to content

Commit 44f6c69

Browse files
Merge pull request #4 from /issues/2
2 根据关键字进行不同的处理
2 parents 2c20f6e + 63c663d commit 44f6c69

File tree

1 file changed

+70
-44
lines changed

1 file changed

+70
-44
lines changed

txtToCsv.py

Lines changed: 70 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import sys
55
import getopt
66

7-
# 抽出したいデータの設備名
8-
DEVICE_CODE = '/dev/input/event4'
7+
# キーワード
8+
KEYWORD_LIST = ['huawei', 'pixcel4a']
99
# 処理ファイルに項目名のindex
1010
KEY_FROM = 50
1111
KEY_TO = 68
@@ -22,7 +22,6 @@
2222
VALUE_TO = 79
2323
# 被験者ID
2424
USER_COLUMN_NAME = '被験者番号'
25-
USER_ID = None # 変更なら文字列で
2625
# ミリ秒値の位置
2726
TIMESTAMP_FROM = 1
2827
TIMESTAMP_TO = 16
@@ -55,63 +54,77 @@
5554
'from': VALUE_FROM,
5655
'to': VALUE_TO,
5756
},
58-
# {
59-
# 'name': '面積(n)',
60-
# 'bind_name': 'ABS_MT_TOUCH_MAJOR',
61-
# 'from': VALUE_FROM,
62-
# 'to': VALUE_TO,
63-
# }
57+
{
58+
'name': '面積(n)',
59+
'bind_name': 'ABS_MT_TOUCH_MAJOR',
60+
'from': VALUE_FROM,
61+
'to': VALUE_TO,
62+
}
6463
]
6564

6665

6766
def get_options(argv):
6867
input_file = ''
6968
output_file = ''
69+
device_code = ''
70+
user_id = ''
7071
try:
71-
opts, args = getopt.getopt(argv, 'hi:o:', ['input_file=', 'output_file='])
72-
except getopt.GetoptError:
73-
print('test.py -i <inputfile> -o <outputfile>')
72+
opts, args = getopt.getopt(argv, 'hi:o:d:u:', ['input_file=', 'output_file=', 'device_code=', 'user_id'])
73+
# パラメータが4個ではない場合、エラーを投げる
74+
if len(opts) != 4:
75+
raise Exception
76+
except Exception:
77+
print('test.py -i <inputfile> -o <outputfile> -d <device_code> -u <user_id>')
7478
sys.exit(2)
7579
for opt, arg in opts:
76-
if opt == '-h':
77-
print('test.py -i <inputfile> -o <outputfile>')
78-
sys.exit()
79-
elif opt in ('-i', '--input_file'):
80+
if opt in ('-i', '--input_file'):
8081
input_file = arg
8182
elif opt in ('-o', '--output_file'):
8283
output_file = arg
83-
if input_file == '' or output_file == '':
84-
print('test.py -i <inputfile> -o <outputfile>')
84+
elif opt in ('-d', '--device_code'):
85+
device_code = arg
86+
elif opt in ('-u', '--user_id'):
87+
user_id = arg
88+
if input_file == '' or output_file == '' or device_code == '' or user_id == '':
89+
print('test.py -i <inputfile> -o <outputfile> -d <device_code> -u <user_id>')
8590
sys.exit()
86-
return [input_file, output_file]
87-
8891

89-
def update_user_id(user_id):
90-
if user_id == None:
91-
user_id = '1'
92-
elif type(user_id) is int:
93-
user_id = str(user_id)
94-
return user_id
92+
return [input_file, output_file, device_code, user_id]
9593

9694

9795
# 処理ファイルから補充済データ、項目名を取得
98-
def create_data_from_file(file):
96+
def create_data_from_file(file_name, keyword, device_code):
9997
data_json = {'TIME': []}
10098
keys = []
10199
i = 0
102-
with open(file, 'r', encoding='utf-8') as f:
100+
101+
if keyword == '':
102+
print('can not found keyword from file name...')
103+
sys.exit()
104+
105+
with open(file_name, 'r', encoding='utf-8') as f:
103106
for row in f.readlines():
104-
if row.find(DEVICE_CODE) > -1 and row.find('add device') == -1:
105-
data_json['TIME'].append(str.strip(row[TIMESTAMP_FROM:TIMESTAMP_TO]))
107+
if row.find(device_code) > -1 and row.find('add device') == -1:
106108
key = str.strip(row[KEY_FROM:KEY_TO])
109+
# pixcel4aの特別のデータを集めないように
110+
if keyword == 'pixcel4a':
111+
if (key == 'ABS_MT_PRESSURE' and row[VALUE_FROM:VALUE_TO] == '00000000') or (
112+
key == 'ABS_MT_TRACKING_ID' and row[VALUE_FROM:VALUE_TO] == 'ffffffff'):
113+
continue
114+
data_json['TIME'].append(str.strip(row[TIMESTAMP_FROM:TIMESTAMP_TO]))
107115
# 生データに項目名及び順番を取得
108116
if not (key in keys):
109117
keys.append(key)
110118

111119
# 省略データがある場合
112120
while key.find(keys[i]) < 0 and i < len(keys):
113121
# 前回のデータを配列に入れる
114-
data_json[keys[i]].append(data_json[keys[i]][-1])
122+
# UPイベントにx軸、y軸以外のデータを0になる
123+
if (data_json[EVENT_NAME][-1].find(EVENT_VALUE_TO) > -1) and (
124+
not keys[i] in ['ABS_MT_POSITION_X', 'ABS_MT_POSITION_Y']):
125+
data_json[keys[i]].append('0')
126+
else:
127+
data_json[keys[i]].append(data_json[keys[i]][-1])
115128
i += 1
116129

117130
if key.find(keys[i]) > -1:
@@ -128,11 +141,23 @@ def create_data_from_file(file):
128141

129142
if i == len(keys) and key == EVENT_STOP_FLAG:
130143
i = 0
144+
if len(keys) == 0:
145+
print('device_code:', device_code, 'is missed...')
146+
sys.exit()
131147
return [data_json, keys]
132148

133149

150+
def get_keyword(file_name):
151+
for keyword in KEYWORD_LIST:
152+
if file_name.lower().find(keyword) == -1:
153+
continue
154+
else:
155+
return keyword
156+
return ''
157+
158+
134159
# データ転置
135-
def data_convert(data_json, keys):
160+
def data_convert(data_json, keys, user_id):
136161
# 一行に最大イベント数
137162
max_event_count_in_row = 0
138163
event_count_in_row = 0
@@ -145,8 +170,8 @@ def data_convert(data_json, keys):
145170
event_count_in_row += 1
146171
for col in COLUMNS:
147172
if col['name'] == USER_COLUMN_NAME and data_json[EVENT_NAME][i].find(EVENT_VALUE_TO) != -1:
148-
row_data.insert(0, USER_ID)
149-
elif col['name'] != USER_COLUMN_NAME:
173+
row_data.insert(0, user_id)
174+
elif col['name'] != USER_COLUMN_NAME and col['bind_name'] in keys:
150175
row_data.append(data_json[col['bind_name']][i])
151176
# イベントがUPの場合
152177
if data_json[EVENT_NAME][i].find(EVENT_VALUE_TO) != -1:
@@ -165,28 +190,29 @@ def data_convert(data_json, keys):
165190
for col in COLUMNS:
166191
if col['name'] == USER_COLUMN_NAME and j == 0:
167192
header.append(USER_COLUMN_NAME)
168-
elif col['name'] != USER_COLUMN_NAME:
193+
elif col['name'] != USER_COLUMN_NAME and col['bind_name'] in keys:
169194
header.append(col['name'].replace('n', str(j + 1)))
170195
result_header.append(','.join(header) + '\n')
171196
return result_header + result_data
172197

173198

174-
def create_file_from_data(data, file):
199+
def create_file_from_data(data, file_name):
175200
now = datetime.now()
176-
[name, extension] = file.split('.')
177-
file_name = name + now.strftime('%Y%m%d%H%M%S%f') + '.' + extension
178-
with open(file_name, 'w', encoding='utf-8') as f:
201+
# ファイル名に最後の「.」で切り分け
202+
[name, extension] = file_name.rsplit('.', 1)
203+
output_file_name = name + now.strftime('%Y%m%d%H%M%S%f') + '.' + extension
204+
with open(output_file_name, 'w', encoding='utf-8') as f:
179205
f.writelines(data)
180206

181207

182208
if __name__ == '__main__':
183209
# コマンドのオプションを取得
184-
[input_file, output_file] = get_options(sys.argv[1:])
185-
# 被験者番号を更新
186-
USER_ID = update_user_id(USER_ID)
210+
[input_file, output_file, device_code, user_id] = get_options(sys.argv[1:])
211+
# キーワードを取得
212+
keyword = get_keyword(input_file)
187213
# 処理ファイルを読み込み
188-
[data_json, keys] = create_data_from_file(input_file)
214+
[data_json, keys] = create_data_from_file(input_file, keyword, device_code)
189215
# データ処理、転置
190-
convert_data = data_convert(data_json, keys)
216+
convert_data = data_convert(data_json, keys, user_id)
191217
# ファイルを出力
192218
create_file_from_data(convert_data, output_file)

0 commit comments

Comments
 (0)