Pythonでテキストファイルを読込んで1行ずつ処理する方法です。
ググってみたら色々な方法が見つかったけど、これが一番シンプルで使いやすかったのでメモする。
サンプルコード
text = open("sample.txt", "r") for line in text: print (line.replace("\n", "")) text.close()
説明
openでテキストファイルを読み込み専用で開き、forで1行ずつ処理します。
replaceで行末の改行を取り除きます。
openの引数「encoding」で文字コードの指定が可能です。
例)encoding="utf-8"
encodingの指定が無い場合はシステムのデフォルトの文字コードになります。
デフォルトの文字コードは下記で確認が可能です。
Python 3.9.0 Type "help", "copyright", "credits" or "license" for more information. >>> import locale >>> locale.getpreferredencoding(False) 'cp932'