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

回答編集履歴

2

ブンポウ

2019/07/23 12:13

投稿

退会済みユーザー
answer CHANGED
@@ -1,9 +1,9 @@
1
1
  ファイルのポインターを変えればいいのではないでしょうか。
2
2
  ```python
3
3
  with open(filepath, "r+" ...) as csvFile:
4
-   csvFile.seek(0)
5
4
    target = list(csvFile.read())
6
5
    target.insert(0, "入れたい文字列")
6
+   csvFile.seek(0)
7
7
    csvFile.write("".join(target))
8
8
 
9
9
  ```

1

修正

2019/07/23 12:13

投稿

退会済みユーザー
answer CHANGED
@@ -1,8 +1,10 @@
1
1
  ファイルのポインターを変えればいいのではないでしょうか。
2
2
  ```python
3
3
  with open(filepath, "r+" ...) as csvFile:
4
- csvFile.seek(0)
4
+   csvFile.seek(0)
5
- csvFile.write()
5
+   target = list(csvFile.read())
6
+   target.insert(0, "入れたい文字列")
7
+   csvFile.write("".join(target))
6
8
 
7
9
  ```
8
10
  で先頭に書き込めますよ。