1
1
# Copyright (c) 2021. 楊鵬. All Rights Reserved.
2
2
3
3
from datetime import datetime
4
+ import sys
5
+ import getopt
4
6
5
- # 処理ファイル名
6
- FROM_FILE = 'testADB_touchMesocket1_log_2020.05.07.txt'
7
7
# 抽出したいデータの設備名
8
8
DEVICE_CODE = '/dev/input/event4'
9
9
# 処理ファイルに項目名のindex
29
29
# 出力列項目
30
30
COLUMNS = [
31
31
{
32
- 'name' : USER_COLUMN_NAME , # 固定
32
+ 'name' : USER_COLUMN_NAME , # 固定
33
33
},
34
34
{
35
35
'name' : 'ミリ秒(n)' ,
62
62
# 'to': VALUE_TO,
63
63
# }
64
64
]
65
- # 出力ファイル名
66
- TO_FILE = 'result.csv'
65
+
66
+
67
+ def get_options (argv ):
68
+ input_file = ''
69
+ output_file = ''
70
+ 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>' )
74
+ sys .exit (2 )
75
+ 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
+ input_file = arg
81
+ elif opt in ('-o' , '--output_file' ):
82
+ output_file = arg
83
+ if input_file == '' or output_file == '' :
84
+ print ('test.py -i <inputfile> -o <outputfile>' )
85
+ sys .exit ()
86
+ return [input_file , output_file ]
67
87
68
88
69
89
def update_user_id (user_id ):
@@ -75,11 +95,11 @@ def update_user_id(user_id):
75
95
76
96
77
97
# 処理ファイルから補充済データ、項目名を取得
78
- def create_data_from_file ():
98
+ def create_data_from_file (file ):
79
99
data_json = {'TIME' : []}
80
100
keys = []
81
101
i = 0
82
- with open (FROM_FILE , 'r' , encoding = 'utf-8' ) as f :
102
+ with open (file , 'r' , encoding = 'utf-8' ) as f :
83
103
for row in f .readlines ():
84
104
if row .find (DEVICE_CODE ) > - 1 and row .find ('add device' ) == - 1 :
85
105
data_json ['TIME' ].append (str .strip (row [TIMESTAMP_FROM :TIMESTAMP_TO ]))
@@ -151,20 +171,23 @@ def data_convert(data_json, keys):
151
171
return result_header + result_data
152
172
153
173
154
- def create_file_from_data (data ):
174
+ def create_file_from_data (data , file ):
155
175
now = datetime .now ()
156
- [name , extension ] = TO_FILE .split ('.' )
176
+ [name , extension ] = file .split ('.' )
157
177
file_name = name + now .strftime ('%Y%m%d%H%M%S%f' ) + '.' + extension
158
178
with open (file_name , 'w' , encoding = 'utf-8' ) as f :
159
179
f .writelines (data )
160
180
161
181
162
182
if __name__ == '__main__' :
183
+ # コマンドのオプションを取得
184
+ [input_file , output_file ] = get_options (sys .argv [1 :])
185
+ print (input_file , output_file )
163
186
# 被験者番号を更新
164
187
USER_ID = update_user_id (USER_ID )
165
188
# 処理ファイルを読み込み
166
- [data_json , keys ] = create_data_from_file ()
189
+ [data_json , keys ] = create_data_from_file (input_file )
167
190
# データ処理、転置
168
191
convert_data = data_convert (data_json , keys )
169
192
# ファイルを出力
170
- create_file_from_data (convert_data )
193
+ create_file_from_data (convert_data , output_file )
0 commit comments