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

回答編集履歴

1

追記

2019/01/18 02:55

投稿

8524ba23
8524ba23

スコア38352

answer CHANGED
@@ -20,4 +20,25 @@
20
20
  fout.close()
21
21
 
22
22
  # out01.txt,out02.txt...が生成される
23
+ ```
24
+ あるいは各データ群をリストに格納すれば、ファイル出力することなく、より簡潔に処理できます。
25
+ ```Python
26
+
27
+ # 1データ群の処理
28
+ def func(lst):
29
+ print(lst[1:]) # 見出し行を除いて出力
30
+
31
+ lst = []
32
+ with open('inp.txt') as f:
33
+ for line in f:
34
+ line = line.rstrip()
35
+ if len(line.split()) == 2: # 出力する行
36
+ lst.append(line)
37
+ else: # 出力行の切り替わり
38
+ if len(lst) > 0:
39
+ func(lst)
40
+ lst = []
41
+
42
+ if len(lst) > 0:
43
+ func(lst)
23
44
  ```