回答編集履歴
1
ファイル書き出し例追加
test
CHANGED
@@ -65,3 +65,83 @@
|
|
65
65
|
id_time[id_] = time
|
66
66
|
|
67
67
|
```
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
ファイル書き出し例
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
```python
|
76
|
+
|
77
|
+
import csv
|
78
|
+
|
79
|
+
from dateutil.parser import parse
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
filename = input("計算するcsvファイル名を入力してください(例:test5.csv):")
|
84
|
+
|
85
|
+
distance = int(input("距離を入力してください(メートル):"))
|
86
|
+
|
87
|
+
unit = input("計算するスピードの単位を決めてください 1:時速 2:分速 3:秒速 ")
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
UNITS = {"1": 3600, "2": 60, "3": 1}
|
92
|
+
|
93
|
+
if unit not in UNITS:
|
94
|
+
|
95
|
+
print("正しい値を入力してください");
|
96
|
+
|
97
|
+
exit();
|
98
|
+
|
99
|
+
unit = UNITS[unit]
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
HEADER_OF_ID = 'id'
|
104
|
+
|
105
|
+
HEADER_OF_TIME = 'datetime'
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def items():
|
110
|
+
|
111
|
+
id_time = {}
|
112
|
+
|
113
|
+
with open(f"./{filename}") as lines:
|
114
|
+
|
115
|
+
header = next(lines).split(',')
|
116
|
+
|
117
|
+
ID = header.index(HEADER_OF_ID)
|
118
|
+
|
119
|
+
TIME = header.index(HEADER_OF_TIME)
|
120
|
+
|
121
|
+
for line in lines:
|
122
|
+
|
123
|
+
items = line.split(',')
|
124
|
+
|
125
|
+
id_ = items[ID]
|
126
|
+
|
127
|
+
time = parse(items[TIME])
|
128
|
+
|
129
|
+
if id_ in id_time:
|
130
|
+
|
131
|
+
yield (id_, distance / ((time - id_time[id_]).seconds / unit))
|
132
|
+
|
133
|
+
del id_time[id_]
|
134
|
+
|
135
|
+
else:
|
136
|
+
|
137
|
+
id_time[id_] = time
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
with open(f"out.csv", "w") as output:
|
142
|
+
|
143
|
+
writer = csv.writer(output, lineterminator="\n")
|
144
|
+
|
145
|
+
writer.writerows(items())
|
146
|
+
|
147
|
+
```
|