LoginSignup
0
0

More than 1 year has passed since last update.

テキストファイル読み込みのすゝめ

Posted at

textファイルはリスト化して読み込み、リストを編集して、リストを書き出し
複数ファイルを扱うなら多次元リスト化する

# textファイルを読み込んでリスト化
file = "import.txt"
with open(file,mode="r",encoding="utf8") as f:
    text_list = f.readlines()

# 何らかの操作
new_text_list = text_list.copy()
new_text_list[0] = "abcdefg\n" # ファイルの一行目を書き換える

# リストをtextファイルとして出力
newfile = "output.txt"
with open(newfile,mode="w",encoding="utf8") as g:
    g.writelines(new_text_list)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0