回答編集履歴

2

Fix answer

2020/07/02 12:38

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -28,22 +28,6 @@
28
28
 
29
29
 
30
30
 
31
- test.txt:
32
-
33
-
34
-
35
- ```text
36
-
37
- 1行目
38
-
39
- 2行目
40
-
41
- 3行目
42
-
43
- ```
44
-
45
-
46
-
47
31
  実行結果:
48
32
 
49
33
 

1

Update answer

2020/07/02 12:38

投稿

y_shinoda
y_shinoda

スコア3272

test CHANGED
@@ -4,21 +4,23 @@
4
4
 
5
5
  ```python
6
6
 
7
- def read_by_line_number(path_to_file, number):
7
+ def read_by_line_number(string, line_number):
8
8
 
9
- with open(path_to_file) as fp:
10
-
11
- for i, line in enumerate(fp):
9
+ return s.splitlines()[line_number - 1]
12
-
13
- if i == number - 1:
14
-
15
- return line
16
-
17
- raise Exception()
18
10
 
19
11
 
20
12
 
13
+
14
+
15
+ s = """1行目
16
+
17
+ 2行目
18
+
19
+ 3行目"""
20
+
21
+
22
+
21
- line = read_by_line_number('test.txt', 2)
23
+ line = read_by_line_number(s, 2)
22
24
 
23
25
  print(line)
24
26