teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コード修正

2018/11/20 09:20

投稿

8524ba23
8524ba23

スコア38352

answer CHANGED
@@ -3,32 +3,41 @@
3
3
  ループは1回で済みますし、メモリも1行分しか必要ありません。
4
4
 
5
5
  ```Python
6
+ fin = open('inp.csv','r')
6
- with open('out.csv','w') as fw: # 合格ファイル名の一覧を出力
7
+ fok = open('ok.csv','w') # 合格
8
+ fng = open('ng.csv','w') # 不合格
7
9
 
8
- # 現在判定対象のファイル名、条件'1'ありか、詳細`succeed`ありか
10
+ # 現在判定対象のファイル名、条件'1'ありか、詳細`succeed`ありか
9
- file,is_one,is_succeed = '',False,False
11
+ file,is_one,is_succeed = '',False,False
10
12
 
11
- with open('inp.csv','r') as fr:
12
- line = fr.readline().strip()
13
+ line = fin.readline().strip()
13
- while line:
14
+ while line:
14
- f,no,desc = line.split(',') # ファイル名,条件,詳細
15
+ f,no,desc = line.split(',') # ファイル名,条件,詳細
15
- line = fr.readline().strip()
16
+ line = fin.readline().strip()
16
17
 
17
- # ファイル名の切り替わり
18
+ # ファイル名の切り替わり
18
- # 現在判定中ファイル名の処理
19
+ # 現在判定中ファイル名の処理
19
- if file != f:
20
+ if file != f:
20
- #print(file,is_one,is_succeed)
21
+ if len(file) > 0:
21
- if len(file) > 0 and (not is_one) and is_succeed:
22
+ if (not is_one) and is_succeed:
22
- fw.write(file+'\n')
23
+ fok.write(file+'\n')
24
+ else:
25
+ fng.write(file+'\n')
23
- file,is_one,is_succeed = f,False,False
26
+ file,is_one,is_succeed = f,False,False
24
27
 
25
- if no == '1':
28
+ if no == '1':
26
- is_one = True
29
+ is_one = True
27
- if desc == 'succeed':
30
+ if desc == 'succeed':
28
- is_succeed = True
31
+ is_succeed = True
32
+ fin.close()
29
33
 
30
- # 最後の行
34
+ # 最後の行
31
- #print(file,is_one,is_succeed)
35
+ if len(file) > 0:
32
- if len(file) > 0 and (not is_one) and is_succeed:
36
+ if (not is_one) and is_succeed:
33
- fw.write(file+'\n')
37
+ fok.write(file+'\n')
38
+ else:
39
+ fng.write(file+'\n')
40
+
41
+ fok.close()
42
+ fng.close()
34
43
  ```