質問編集履歴
2
コードの修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -116,45 +116,281 @@
|
|
116
116
|
|
117
117
|
|
118
118
|
|
119
|
+
ITEMS_DATA = 'input/items.csv'
|
120
|
+
|
121
|
+
ITEMS_COLUMNS = ['item_id', 'name', 'price']
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
SALES_RAW_REGEX = re.compile(r'^sales_raw_(\d{4})(\d{2})(\d{2}).csv$')
|
126
|
+
|
127
|
+
SALES_COLUMNS = ['purchase_id', 'user_id', 'item_id',
|
128
|
+
|
129
|
+
'item_name', 'item_price', 'amount', 'sold_at']
|
130
|
+
|
131
|
+
SALES_DATA = 'output/sales.csv'
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
ENCODING = 'utf-8'
|
136
|
+
|
137
|
+
CSV_INPUT = input('')
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
# CSVファイルを読み込んで辞書のリストで返す関数を実装する
|
142
|
+
|
143
|
+
def read_csv(f, columns):
|
144
|
+
|
145
|
+
with open(os.path.join('input/', filename), encoding=ENCODING) as f:
|
146
|
+
|
147
|
+
for row in f:
|
148
|
+
|
149
|
+
data = row.rstrip().split(',')
|
150
|
+
|
151
|
+
|
152
|
+
|
153
|
+
# Step5では以下の状態で実装する
|
154
|
+
|
155
|
+
def write_csv(f, data, columns):
|
156
|
+
|
157
|
+
pass
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
# Step5では以下の状態で実装する
|
162
|
+
|
163
|
+
def read_items():
|
164
|
+
|
165
|
+
pass
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
# Step5では以下の状態で実装する
|
170
|
+
|
171
|
+
def read_sales_raw(target_year, target_month):
|
172
|
+
|
173
|
+
pass
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
# Step5では以下の状態で実装する
|
178
|
+
|
179
|
+
def write_sales(sales):
|
180
|
+
|
181
|
+
pass
|
182
|
+
|
183
|
+
|
184
|
+
|
119
185
|
def main():
|
120
186
|
|
121
|
-
print('csvファイルを入力してください')
|
122
|
-
|
123
|
-
csv_input = input('')
|
124
|
-
|
125
|
-
SALES_RAW_REGEX = re.compile(r'^sales_raw_(\d{4})(\d{2})(\d{2}).csv$')
|
126
|
-
|
127
|
-
# 商品マスターデータ
|
187
|
+
# 商品のマスターデータ読み込み
|
128
188
|
|
129
189
|
items = {}
|
130
190
|
|
131
|
-
with open(
|
191
|
+
with open(ITEMS_DATA, encoding=ENCODING) as f:
|
192
|
+
|
193
|
+
# CSVファイルを読み込んで辞書のリストで返す関数使う
|
132
194
|
|
133
195
|
for row in f:
|
134
196
|
|
135
|
-
item_id, name, price = row.rstrip().split(',')
|
197
|
+
item_id, item_name, item_price = row.rstrip().split(',')
|
136
198
|
|
137
199
|
items[item_id] = {
|
138
200
|
|
139
|
-
'name': name,
|
201
|
+
'item_name': item_name,
|
140
|
-
|
202
|
+
|
141
|
-
'price': price
|
203
|
+
'item_price': item_price
|
142
204
|
|
143
205
|
}
|
144
206
|
|
145
207
|
|
146
208
|
|
147
|
-
# 売上生データ
|
209
|
+
# 売上の生データを読み込んで、まとめる
|
148
210
|
|
149
211
|
sales = []
|
150
212
|
|
151
213
|
for filename in os.listdir('input/'):
|
152
214
|
|
215
|
+
# * 対象の月のデータのみ読み込み
|
216
|
+
|
153
|
-
if re.match(
|
217
|
+
if re.match(CSV_INPUT, filename):
|
154
|
-
|
218
|
+
|
155
|
-
with open(os.path.join('input/', filename), encoding=
|
219
|
+
with open(os.path.join('input/', filename), encoding=ENCODING) as f:
|
220
|
+
|
156
|
-
|
221
|
+
for row in read_csv(f, data):
|
222
|
+
|
223
|
+
# 商品の情報を追加する
|
224
|
+
|
225
|
+
item_id = data[2]
|
226
|
+
|
227
|
+
if item_id in items:
|
228
|
+
|
229
|
+
sales.append({
|
230
|
+
|
231
|
+
'purchase_id': data[0],
|
232
|
+
|
233
|
+
'user_id': data[1],
|
234
|
+
|
235
|
+
'item_id': data[2],
|
236
|
+
|
237
|
+
'item_name': items[item_id]['item_name'],
|
238
|
+
|
239
|
+
'item_price': items[item_id]['item_price'],
|
240
|
+
|
241
|
+
'amount': data[3],
|
242
|
+
|
243
|
+
'sold_at': data[4]
|
244
|
+
|
245
|
+
})
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
# まとめた売上データを書き出し
|
250
|
+
|
251
|
+
m = SALES_RAW_REGEX.search(filename)
|
252
|
+
|
253
|
+
if len(sales) == 0:
|
254
|
+
|
255
|
+
print('書き出しファイルがありません')
|
256
|
+
|
257
|
+
return
|
258
|
+
|
259
|
+
SALES_COLUMNS
|
260
|
+
|
261
|
+
with open(SALES_DATA, mode='w', encoding=ENCODING) as f:
|
262
|
+
|
263
|
+
for row in sales:
|
264
|
+
|
265
|
+
row_str = ','.join(str(row[column]) for column in SALES_COLUMNS)
|
266
|
+
|
267
|
+
f.write(row_str + '\n')
|
268
|
+
|
269
|
+
|
270
|
+
|
271
|
+
if __name__ == "__main__":
|
272
|
+
|
273
|
+
main()
|
274
|
+
|
275
|
+
```
|
276
|
+
|
277
|
+
|
278
|
+
|
279
|
+
### 改修途中のプログラム
|
280
|
+
|
281
|
+
```py
|
282
|
+
|
283
|
+
import os
|
284
|
+
|
285
|
+
import re
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
ITEMS_DATA = 'input/items.csv'
|
290
|
+
|
291
|
+
ITEMS_COLUMNS = ['item_id', 'name', 'price']
|
292
|
+
|
293
|
+
|
294
|
+
|
295
|
+
SALES_RAW_REGEX = re.compile(r'^sales_raw_(\d{4})(\d{2})(\d{2}).csv$')
|
296
|
+
|
297
|
+
SALES_COLUMNS = ['purchase_id', 'user_id', 'item_id',
|
298
|
+
|
299
|
+
'item_name', 'item_price', 'amount', 'sold_at']
|
300
|
+
|
301
|
+
SALES_DATA = 'output/sales.csv'
|
302
|
+
|
303
|
+
|
304
|
+
|
305
|
+
ENCODING = 'utf-8'
|
306
|
+
|
307
|
+
CSV_INPUT = input('')
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
# CSVファイルを読み込んで辞書のリストで返す関数を実装する
|
312
|
+
|
313
|
+
def read_csv(f, columns):
|
314
|
+
|
315
|
+
with open(os.path.join('input/', filename), encoding=ENCODING) as f:
|
316
|
+
|
157
|
-
|
317
|
+
for row in f:
|
318
|
+
|
319
|
+
data = row.rstrip().split(',')
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
# Step5では以下の状態で実装する
|
324
|
+
|
325
|
+
def write_csv(f, data, columns):
|
326
|
+
|
327
|
+
pass
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
# Step5では以下の状態で実装する
|
332
|
+
|
333
|
+
def read_items():
|
334
|
+
|
335
|
+
pass
|
336
|
+
|
337
|
+
|
338
|
+
|
339
|
+
# Step5では以下の状態で実装する
|
340
|
+
|
341
|
+
def read_sales_raw(target_year, target_month):
|
342
|
+
|
343
|
+
pass
|
344
|
+
|
345
|
+
|
346
|
+
|
347
|
+
# Step5では以下の状態で実装する
|
348
|
+
|
349
|
+
def write_sales(sales):
|
350
|
+
|
351
|
+
pass
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
def main():
|
356
|
+
|
357
|
+
# 商品のマスターデータ読み込み
|
358
|
+
|
359
|
+
items = {}
|
360
|
+
|
361
|
+
with open(ITEMS_DATA, encoding=ENCODING) as f:
|
362
|
+
|
363
|
+
# CSVファイルを読み込んで辞書のリストで返す関数使う
|
364
|
+
|
365
|
+
for row in f:
|
366
|
+
|
367
|
+
item_id, item_name, item_price = row.rstrip().split(',')
|
368
|
+
|
369
|
+
items[item_id] = {
|
370
|
+
|
371
|
+
'item_name': item_name,
|
372
|
+
|
373
|
+
'item_price': item_price
|
374
|
+
|
375
|
+
}
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
# 売上の生データを読み込んで、まとめる
|
380
|
+
|
381
|
+
sales = []
|
382
|
+
|
383
|
+
for filename in os.listdir('input/'):
|
384
|
+
|
385
|
+
# * 対象の月のデータのみ読み込み
|
386
|
+
|
387
|
+
if re.match(CSV_INPUT, filename):
|
388
|
+
|
389
|
+
with open(os.path.join('input/', filename), encoding=ENCODING) as f:
|
390
|
+
|
391
|
+
for row in read_csv(f, data):
|
392
|
+
|
393
|
+
# 商品の情報を追加する
|
158
394
|
|
159
395
|
data = row.rstrip().split(',')
|
160
396
|
|
@@ -170,11 +406,11 @@
|
|
170
406
|
|
171
407
|
'item_id': data[2],
|
172
408
|
|
173
|
-
'name': items[item_id]['name'],
|
409
|
+
'item_name': items[item_id]['item_name'],
|
174
|
-
|
410
|
+
|
175
|
-
'price': items[item_id]['price'],
|
411
|
+
'item_price': items[item_id]['item_price'],
|
176
|
-
|
412
|
+
|
177
|
-
'
|
413
|
+
'amount': data[3],
|
178
414
|
|
179
415
|
'sold_at': data[4]
|
180
416
|
|
@@ -184,7 +420,7 @@
|
|
184
420
|
|
185
421
|
# まとめた売上データを書き出し
|
186
422
|
|
187
|
-
m = SALES_RAW_REGEX.search(name)
|
423
|
+
m = SALES_RAW_REGEX.search(filename)
|
188
424
|
|
189
425
|
if len(sales) == 0:
|
190
426
|
|
@@ -192,15 +428,13 @@
|
|
192
428
|
|
193
429
|
return
|
194
430
|
|
195
|
-
|
431
|
+
SALES_COLUMNS
|
196
|
-
|
197
|
-
|
432
|
+
|
198
|
-
|
199
|
-
with open(
|
433
|
+
with open(SALES_DATA, mode='w', encoding=ENCODING) as f:
|
200
434
|
|
201
435
|
for row in sales:
|
202
436
|
|
203
|
-
row_str = ','.join(str(row[column]) for column in
|
437
|
+
row_str = ','.join(str(row[column]) for column in SALES_COLUMNS)
|
204
438
|
|
205
439
|
f.write(row_str + '\n')
|
206
440
|
|
@@ -214,178 +448,6 @@
|
|
214
448
|
|
215
449
|
|
216
450
|
|
217
|
-
### 改修途中のプログラム
|
218
|
-
|
219
|
-
```py
|
220
|
-
|
221
|
-
import os
|
222
|
-
|
223
|
-
import re
|
224
|
-
|
225
|
-
|
226
|
-
|
227
|
-
ITEMS_DATA = 'input/items.csv'
|
228
|
-
|
229
|
-
ITEMS_COLUMNS = ['item_id', 'name', 'price']
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
SALES_RAW_REGEX = re.compile(r'^sales_raw_(\d{4})(\d{2})(\d{2}).csv$')
|
234
|
-
|
235
|
-
SALES_COLUMNS = ['purchase_id', 'user_id', 'item_id',
|
236
|
-
|
237
|
-
'item_name', 'item_price', 'amount', 'sold_at']
|
238
|
-
|
239
|
-
SALES_DATA = 'output/sales.csv'
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
ENCODING = 'utf-8'
|
244
|
-
|
245
|
-
CSV_INPUT = input('')
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
# CSVファイルを読み込んで辞書のリストで返す関数を実装する
|
250
|
-
|
251
|
-
def read_csv(f, columns):
|
252
|
-
|
253
|
-
with open(os.path.join('input/', filename), encoding=ENCODING) as f:
|
254
|
-
|
255
|
-
for row in f:
|
256
|
-
|
257
|
-
data = row.rstrip().split(',')
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
# Step5では以下の状態で実装する
|
262
|
-
|
263
|
-
def write_csv(f, data, columns):
|
264
|
-
|
265
|
-
pass
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
# Step5では以下の状態で実装する
|
270
|
-
|
271
|
-
def read_items():
|
272
|
-
|
273
|
-
pass
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
# Step5では以下の状態で実装する
|
278
|
-
|
279
|
-
def read_sales_raw(target_year, target_month):
|
280
|
-
|
281
|
-
pass
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
# Step5では以下の状態で実装する
|
286
|
-
|
287
|
-
def write_sales(sales):
|
288
|
-
|
289
|
-
pass
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
def main():
|
294
|
-
|
295
|
-
# 商品のマスターデータ読み込み
|
296
|
-
|
297
|
-
items = {}
|
298
|
-
|
299
|
-
with open(ITEMS_DATA, encoding=ENCODING) as f:
|
300
|
-
|
301
|
-
# CSVファイルを読み込んで辞書のリストで返す関数使う
|
302
|
-
|
303
|
-
for row in f:
|
304
|
-
|
305
|
-
item_id, item_name, item_price = row.rstrip().split(',')
|
306
|
-
|
307
|
-
items[item_id] = {
|
308
|
-
|
309
|
-
'item_name': item_name,
|
310
|
-
|
311
|
-
'item_price': item_price
|
312
|
-
|
313
|
-
}
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
# 売上の生データを読み込んで、まとめる
|
318
|
-
|
319
|
-
sales = []
|
320
|
-
|
321
|
-
for filename in os.listdir('input/'):
|
322
|
-
|
323
|
-
# * 対象の月のデータのみ読み込み
|
324
|
-
|
325
|
-
if re.match(CSV_INPUT, filename):
|
326
|
-
|
327
|
-
with open(os.path.join('input/', filename), encoding=ENCODING) as f:
|
328
|
-
|
329
|
-
for row in read_csv(f, data):
|
330
|
-
|
331
|
-
# 商品の情報を追加する
|
332
|
-
|
333
|
-
data = row.rstrip().split(',')
|
334
|
-
|
335
|
-
item_id = data[2]
|
336
|
-
|
337
|
-
if item_id in items:
|
338
|
-
|
339
|
-
sales.append({
|
340
|
-
|
341
|
-
'purchase_id': data[0],
|
342
|
-
|
343
|
-
'user_id': data[1],
|
344
|
-
|
345
|
-
'item_id': data[2],
|
346
|
-
|
347
|
-
'item_name': items[item_id]['item_name'],
|
348
|
-
|
349
|
-
'item_price': items[item_id]['item_price'],
|
350
|
-
|
351
|
-
'amount': data[3],
|
352
|
-
|
353
|
-
'sold_at': data[4]
|
354
|
-
|
355
|
-
})
|
356
|
-
|
357
|
-
|
358
|
-
|
359
|
-
# まとめた売上データを書き出し
|
360
|
-
|
361
|
-
m = SALES_RAW_REGEX.search(filename)
|
362
|
-
|
363
|
-
if len(sales) == 0:
|
364
|
-
|
365
|
-
print('書き出しファイルがありません')
|
366
|
-
|
367
|
-
return
|
368
|
-
|
369
|
-
SALES_COLUMNS
|
370
|
-
|
371
|
-
with open(SALES_DATA, mode='w', encoding=ENCODING) as f:
|
372
|
-
|
373
|
-
for row in sales:
|
374
|
-
|
375
|
-
row_str = ','.join(str(row[column]) for column in SALES_COLUMNS)
|
376
|
-
|
377
|
-
f.write(row_str + '\n')
|
378
|
-
|
379
|
-
|
380
|
-
|
381
|
-
if __name__ == "__main__":
|
382
|
-
|
383
|
-
main()
|
384
|
-
|
385
|
-
```
|
386
|
-
|
387
|
-
|
388
|
-
|
389
451
|
### 自分で試したこと
|
390
452
|
|
391
453
|
・繰り返し使用されるコードに関しては、定数を定義しました。
|
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -10,6 +10,30 @@
|
|
10
10
|
|
11
11
|
共通の処理やまとまりを関数に分離する、定数化できるところは定数化する
|
12
12
|
|
13
|
+
「# CSVファイルを読み込んで辞書のリストで返す関数を実装する」の箇所で関数を用いて実行してみましたが、
|
14
|
+
|
15
|
+
下記のネームエラーが出てしまいます。
|
16
|
+
|
17
|
+
エラーでは、dataが定義されていないとのことですが、関数を呼び出している「# * 対象の月のデータのみ読み込み」
|
18
|
+
|
19
|
+
の引数に問題があるのでしょうか?
|
20
|
+
|
21
|
+
```
|
22
|
+
|
23
|
+
Traceback (most recent call last):
|
24
|
+
|
25
|
+
File "/Users/******/docker/src/main.py", line 81, in <module>
|
26
|
+
|
27
|
+
main()
|
28
|
+
|
29
|
+
File "/Users/******/docker/src/main.py", line 55, in main
|
30
|
+
|
31
|
+
for row in read_csv(data):
|
32
|
+
|
33
|
+
NameError: name 'data' is not defined
|
34
|
+
|
35
|
+
```
|
36
|
+
|
13
37
|
|
14
38
|
|
15
39
|
### ファイル等の情報
|
@@ -226,13 +250,25 @@
|
|
226
250
|
|
227
251
|
def read_csv(f, columns):
|
228
252
|
|
253
|
+
with open(os.path.join('input/', filename), encoding=ENCODING) as f:
|
254
|
+
|
255
|
+
for row in f:
|
256
|
+
|
257
|
+
data = row.rstrip().split(',')
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
# Step5では以下の状態で実装する
|
262
|
+
|
263
|
+
def write_csv(f, data, columns):
|
264
|
+
|
229
265
|
pass
|
230
266
|
|
231
267
|
|
232
268
|
|
233
269
|
# Step5では以下の状態で実装する
|
234
270
|
|
235
|
-
def
|
271
|
+
def read_items():
|
236
272
|
|
237
273
|
pass
|
238
274
|
|
@@ -240,7 +276,7 @@
|
|
240
276
|
|
241
277
|
# Step5では以下の状態で実装する
|
242
278
|
|
243
|
-
def read_
|
279
|
+
def read_sales_raw(target_year, target_month):
|
244
280
|
|
245
281
|
pass
|
246
282
|
|
@@ -248,20 +284,12 @@
|
|
248
284
|
|
249
285
|
# Step5では以下の状態で実装する
|
250
286
|
|
251
|
-
def re
|
287
|
+
def write_sales(sales):
|
252
288
|
|
253
289
|
pass
|
254
290
|
|
255
291
|
|
256
292
|
|
257
|
-
# Step5では以下の状態で実装する
|
258
|
-
|
259
|
-
def write_sales(sales):
|
260
|
-
|
261
|
-
pass
|
262
|
-
|
263
|
-
|
264
|
-
|
265
293
|
def main():
|
266
294
|
|
267
295
|
# 商品のマスターデータ読み込み
|
@@ -296,11 +324,11 @@
|
|
296
324
|
|
297
325
|
if re.match(CSV_INPUT, filename):
|
298
326
|
|
299
|
-
# 商品の情報を追加する
|
300
|
-
|
301
327
|
with open(os.path.join('input/', filename), encoding=ENCODING) as f:
|
302
328
|
|
303
|
-
for row in f:
|
329
|
+
for row in read_csv(f, data):
|
330
|
+
|
331
|
+
# 商品の情報を追加する
|
304
332
|
|
305
333
|
data = row.rstrip().split(',')
|
306
334
|
|