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

Skip to content

Commit 2c20f6e

Browse files
Merge pull request #3 from /issues/1
1 增加【读取命令行参数】的功能
2 parents c5d8b04 + f562471 commit 2c20f6e

File tree

1 file changed

+33
-11
lines changed

1 file changed

+33
-11
lines changed

txtToCsv.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# Copyright (c) 2021. 楊鵬. All Rights Reserved.
22

33
from datetime import datetime
4+
import sys
5+
import getopt
46

5-
# 処理ファイル名
6-
FROM_FILE = 'testADB_touchMesocket1_log_2020.05.07.txt'
77
# 抽出したいデータの設備名
88
DEVICE_CODE = '/dev/input/event4'
99
# 処理ファイルに項目名のindex
@@ -29,7 +29,7 @@
2929
# 出力列項目
3030
COLUMNS = [
3131
{
32-
'name': USER_COLUMN_NAME, # 固定
32+
'name': USER_COLUMN_NAME, # 固定
3333
},
3434
{
3535
'name': 'ミリ秒(n)',
@@ -62,8 +62,28 @@
6262
# 'to': VALUE_TO,
6363
# }
6464
]
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]
6787

6888

6989
def update_user_id(user_id):
@@ -75,11 +95,11 @@ def update_user_id(user_id):
7595

7696

7797
# 処理ファイルから補充済データ、項目名を取得
78-
def create_data_from_file():
98+
def create_data_from_file(file):
7999
data_json = {'TIME': []}
80100
keys = []
81101
i = 0
82-
with open(FROM_FILE, 'r', encoding='utf-8') as f:
102+
with open(file, 'r', encoding='utf-8') as f:
83103
for row in f.readlines():
84104
if row.find(DEVICE_CODE) > -1 and row.find('add device') == -1:
85105
data_json['TIME'].append(str.strip(row[TIMESTAMP_FROM:TIMESTAMP_TO]))
@@ -151,20 +171,22 @@ def data_convert(data_json, keys):
151171
return result_header + result_data
152172

153173

154-
def create_file_from_data(data):
174+
def create_file_from_data(data, file):
155175
now = datetime.now()
156-
[name, extension] = TO_FILE.split('.')
176+
[name, extension] = file.split('.')
157177
file_name = name + now.strftime('%Y%m%d%H%M%S%f') + '.' + extension
158178
with open(file_name, 'w', encoding='utf-8') as f:
159179
f.writelines(data)
160180

161181

162182
if __name__ == '__main__':
183+
# コマンドのオプションを取得
184+
[input_file, output_file] = get_options(sys.argv[1:])
163185
# 被験者番号を更新
164186
USER_ID = update_user_id(USER_ID)
165187
# 処理ファイルを読み込み
166-
[data_json, keys] = create_data_from_file()
188+
[data_json, keys] = create_data_from_file(input_file)
167189
# データ処理、転置
168190
convert_data = data_convert(data_json, keys)
169191
# ファイルを出力
170-
create_file_from_data(convert_data)
192+
create_file_from_data(convert_data, output_file)

0 commit comments

Comments
 (0)