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

回答編集履歴

3

エラー原因について説明追記

2020/05/08 12:47

投稿

teamikl
teamikl

スコア8817

answer CHANGED
@@ -36,4 +36,14 @@
36
36
  w = csv.writer(f)
37
37
  for note in notes:
38
38
  w.writerow([note.start, note.end, note.pitch, note.velocity])
39
+
40
+ # XXX: w.writerows(notes) がエラー原因。理由は先述の通りデータの変換が必要
41
+
42
+ # with open() ~では、
43
+ # インデントで表現されたブロックの終端でファイルが閉じます。
44
+ # ブロックの終端は、下の for 文でインデントがwith~と同じ位置に戻る箇所。
45
+
46
+ for note in notes:
47
+ # ここでの w.writerow は、ファイルが閉じられてるので無効。
48
+ print(note)
39
49
  ```

2

w.writerow を呼び出す位置の確認用コードを追記

2020/05/08 12:47

投稿

teamikl
teamikl

スコア8817

answer CHANGED
@@ -25,4 +25,15 @@
25
25
  ## (3) 何度も note. と書きたくない場合
26
26
  from operators import attrgetter
27
27
  w.writerows(map(attrgetter("start", "end", "pitch", "velocity"), notes))
28
+ ```
29
+
30
+
31
+ ----
32
+ 追記: 実行確認はしてないのでコピペの際は注意。w.writenow の追加位置確認用です。
33
+
34
+ ```python
35
+ with open("new.csv", "w", newline="") as f:
36
+ w = csv.writer(f)
37
+ for note in notes:
38
+ w.writerow([note.start, note.end, note.pitch, note.velocity])
28
39
  ```

1

誤字修正

2020/05/08 12:34

投稿

teamikl
teamikl

スコア8817

answer CHANGED
@@ -1,4 +1,4 @@
1
- notes の中身は確認してませんが、csv.writerows の使い方は
1
+ notes の中身は確認してませんが、writerows の使い方は
2
2
 
3
3
  ```python
4
4
  # with ステートメントで記述すると、close()は必要に応じて自動的に呼ばれます。