回答編集履歴

1

Update

2022/01/05 12:55

投稿

melian
melian

スコア19865

test CHANGED
@@ -1,8 +1,10 @@
1
- 入力ファイルを `call.dat` として以下の様に書いたので参考にしてみて下さい(出力 `stdout` )
1
+ 以下の様に書いたので参考にしてみて下さい。入力ファイルを `call.dat`, 出力ファイルを `called.dat` にしています。
2
2
 
3
3
  ```python
4
4
 
5
5
  import re
6
+
7
+ from contextlib import redirect_stdout
6
8
 
7
9
 
8
10
 
@@ -32,16 +34,18 @@
32
34
 
33
35
  # output
34
36
 
35
- for c in call:
37
+ with open('called.dat', 'w') as f, redirect_stdout(f):
36
38
 
37
- first, rest = c.split('\n', 1)
39
+ for c in call:
38
40
 
39
- t = first.split(' ')[1]
41
+ first, rest = c.split('\n', 1)
40
42
 
41
- print(first)
43
+ t = first.split(' ')[1]
42
44
 
43
- if t in tag: print(t, tag[t], sep='\n')
45
+ print(first)
44
46
 
47
+ if t in tag: print(t, tag[t], sep='\n')
48
+
45
- print(rest)
49
+ print(rest)
46
50
 
47
51
  ```