LoginSignup
4
4

More than 5 years have passed since last update.

Pythonファイル入出力用メモ

Last updated at Posted at 2015-07-21

業務でcsv加工系の処理を書きそうなのでカンペ作成"φ(・ェ・o)~メモメモ
2系のつもりで3系混ざっていたらすみません。

open関数

file = open("read.txt", "r", "UTF-8")
引数 必須 説明
$1 ファイル名
$2 - モード
$3 - 文字コード

モードのオプションは複数指定が可能。

タイプ 説明
r 読み取り専用
a 追記、ファイルがない場合は生成
t テキストモード(default)
b バイナリモード

withステートメントを使用すると、自動でclose()が可能。

with open('text_file.txt') as file:
    print(file.read())

連結

# join関数で簡単に連結
"-".join(["foo", "bar", "baz"])
>>> 'foo-bar-baz'

変換

# "%s"で置き換えも楽チンに
"%s, %s, %s!" % ("one", "two", "three")
>>> 'one, two, three!'
4
4
3

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
4
4