=====================================================================================
<< BACK >>
=====================================================================================
文字列を特定の文字(区切り文字)で分割
1. 基本的な分割 (split)
text = "apple,orange,banana"
fruits = text.split(",")
print(fruits)
# 出力: ['apple', 'orange', 'banana']
2. 空白で分割する場合
引数を指定せずに split() を使用。スペース、タブ、改行などの連続する空白文字をまとめて区切りとして扱います。
text = "Python Java Ruby"
languages = text.split()
print(languages)
# 出力: ['Python', 'Java', 'Ruby']
3. 分割回数を制限する (maxsplit)
第2引数に数値を渡すと、分割する回数を指定できます。データの「最初の1つだけ切り出したい」時に便利です。
text = "2023/10/27/entry/123"
data = text.split("/", 2)
print(data)
# 出力: ['2023', '10', '27/entry/123'] (2回だけ分割される)
4. 複数の異なる文字で分割したい場合
, や / など、複数の区切り文字が混在している場合は、標準ライブラリの re モジュール(正規表現) を使います。
import re
text = "apple,orange;banana blue"
# カンマ(,) セミコロン(;) スペース( ) のいずれかで分割
fruits = re.split(r'[,; ]', text)
print(fruits)
# 出力: ['apple', 'orange', 'banana', 'blue']
まとめ
やりたいこと 使うメソッド 特徴
特定の1文字で分ける text.split(',') 最も標準的
空白・改行で分ける text.split() 連続した空白も1つとして扱う
後ろから分ける text.rsplit(',', 1) 右側から指定回数分だけ分ける
複数種類の文字で分ける re.split(r'[; /]', text) 正規表現で柔軟に指定可能
=====================================================================================
演算子
演算子 意味 例 (a=10, b=3) 結果
+ 加算 a + b 13
- 減算 a - b 7
* 乗算 a * b 30
/ 除算 a / b 3.333...
// 切り捨て除算 a // b 3
% 剰余(あまり)a % b 1
** べき乗 a ** b 1000
== 等しい a == b
!= 等しくない a != b
> より大きい a > b
< より小さい a < b
>= 以上 a >= b
<= 以下 a <= b
= x = 5 x に 5 を代入
+= x += 3 x = x + 3
-= x -= 2 x = x - 2
*= x *= 4 x = x * 4
/= x /= 2 x = x / 2
and 論理積 すべての条件が True なら True
if score >= 80 and attendance >= 80:
or 論理和 どれか一つでも True なら True
if is_weekend or is_holiday:
not 論理否定 真理値を反転させる(True → False)
if not is_error:
is_error = False / is_error = not is_error -> is_errorはTrue
=====================================================================================
ファイルの読み書き
読み
全文を一度に読み込む
with open('log.txt', 'r', encoding='utf-8') as f:
data = f.read()
print(data)
1行ずつリストとして読み込む
f.readlines():一度に全部読み込みline.strip()で分割
f.readline()なら1行のみ読み込み
with open('config.txt', 'r', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
print(line.strip()) # strip()で改行文字を除去
末尾に改行コード(\n)を取り除く
1. .rstrip() を使う
.rstrip():「右側の文字を取り除く」。引数に何も指定しない場合、末尾の改行やスペースをすべて削除します。
with open("sample.txt", "r") as f:
line = f.readline().rstrip("\n") # 改行コードだけを確実に消す
print(line)
2. .strip() を使う(前後の空白も消す場合)
改行だけでなく、行の前後にある不要なスペースも一緒に消したい場合に便利です。
line = f.readline().strip()
3. スライスを使う
最後の1文字を削ることもできます。
line = f.readline()[:-1]
Fileの終わり
空(EOF)が返って来る。
ファイルポインタが末尾(EOF)に達したことを if 文で判断するには、**「読み取った結果が空(長さ0)であるか」**をチェックします。
もっとも一般的な判定方法です。Pythonでは空文字は「偽(False)」と判定されるため、if not で簡潔に書けます。
line = f.readline()
if not line:
# 読み込んだ結果が '' (空文字) だった場合 = EOF
print("ファイルの終わりに達しました")
書き
中身をすべて上書き
with open('status.txt', 'w', encoding='utf-8') as f:
f.write("REC_START")
追記
import datetime
# 'a' は Append(追記)の略
with open('log.txt', 'a', encoding='utf-8') as f:
now = datetime.datetime.now()
f.write(f"{now}: 録画を開始しました\n") # \n は改行
リストをまとめて書き込む
lines = ["apple\n", "banana\n", "cherry\n"]
with open('list.txt', 'w', encoding='utf-8') as f:
f.writelines(lines)
よく使うオプション(モード)
open() の第2引数で指定します。
'r': 読み込み(デフォルト)。ファイルがないとエラー。'w': 書き込み。既存の内容は消去されます。'a': 追記。既存の内容の末尾に書き足します。'rb': バイナリ読み込み(画像や動画ファイルを扱うとき)。'wb': バイナリ書き込み(画像や動画ファイルを扱うとき)。'ab': バイナリ追記。既存の内容の末尾に書き足します。ストリーミングの記録等'xb': 誤上書きの防止。同じ名前のファイルが既にあったらエラーにして上書きを阻止- FileExistsError という「例外(Exception)」を発生
try:
with open(‘important_data.bin’, ‘xb’) as f:
f.write(data)
except FileExistsError:
print(“エラー:同名のファイルが既に存在します!”)
- FileExistsError という「例外(Exception)」を発生
注意点
- 文字コード: Windowsで作ったファイルをラズパイ(Linux)で読む場合などは、
encoding='utf-8'を明示的に指定しないと文字化けすることがあります。 - パス: ファイルが実行中のPythonファイルと同じフォルダにない場合は、フルパス(例:
/home/pi/data.txt)を指定してください。 f.write()に渡せるのは 文字列(String)だけ です。数値を書き込みたい場合は、必ずstr()で変換するか、f-string(f"{num}")を使ってください- NG:
f.write(123)OK:f.write(str(123))
- NG:
バイナリモードの重要なルール
encodingを指定してはいけないopen('file', 'wb', encoding='utf-8')と書くと、Pythonは「バイナリ(数値の羅列)なのに文字コードがあるのはおかしい」と判断し、エラー(ValueError)を投げます。- 書き込むデータは
bytes型であること 文字列(str型)をそのままf.write()することはできません。文字列をバイナリとして書きたい場合は.encode()が必須です。- # 成功例
f.write(b’\x00\x01\x02′) # byte列
f.write(“text”.encode()) # bytesに変換されたデータ
# 失敗例
f.write(“text”)
TypeError: a bytes-like object is required, not ‘str’
- # 成功例
=====================================================================================
GPIO制御
import RPi.GPIO as GPIO
# ピン番号の数え方を指定(BOARD: 基板の端子順 / BCM: チップの番号)
GPIO.setmode(GPIO.BCM)
# 使用するピン(例:18番ピン)を出力モードに設定
GPIO.setup(18, GPIO.OUT)
# 出力をONにする(3.3V出力)
GPIO.output(18, GPIO.HIGH)
# 24番ピンを入力に設定。内部プルアップを有効にする。
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# ピンの状態を読み込む
input_state = GPIO.input(24)
# 最後に必ずお掃除(これを忘れると次に使うときに警告が出ます)
GPIO.cleanup()
=====================================================================================
複数行の文字列定義
3つのダブルまたはシングルコーテーション ”””…””” or ”’…”’ を使用
multiline_string = """
This is a string that
spans multiple lines.
Newlines and indentation are preserved.
"""
print(multiline_string)
This is a string that
spans multiple lines.
Newlines and indentation are preserved.
\(バックスラッシュ)を使用
single_line_string = "This is a long string that is split across " \
"multiple lines in the source code."
print(single_line_string)
This is a long string that is split across multiple lines in the source code.
# To include newlines in the output:
string_with_breaks = "Line 1\n" \
"Line 2\n" \
"Line 3"
print(string_with_breaks)
Line 1
Line 2
Line 3
()を使用
concatenated_string = ("This text will be joined into a single line. "
"Spaces and newlines in the source code "
"between the quotes are ignored.")
print(concatenated_string)
This text will be joined into a single line. Spaces and newlines in the source code between the quotes are ignored.
=====================================================================================
get()
辞書から値を取得する
user = {"name": "John", "age": 30}
name = user.get("name")
name
"John"
無いキーを指定 -> Noneが帰って来る Errorでは無い
email = user.get("email")
print(email)
None
=====================================================================================
os.path.splitext()
path_stringを要素に分割
import os
path='/path/to/file.jpg'
root, extension = os.path.splitext(path)
print(root)
/path/to/file
print(extension)
.jpg
path_array = os.path.splitext(path)
path_array
('/path/to/file', '.jpg')
=====================================================================================
urlparse()
urlparse(): URL を部分文字列に分解するための機能
from urllib.parse import urlparse
url = "http://example.com/path/to/file.jpg?param=1"
parsed = urlparse(url)
print(parsed)
ParseResult(scheme='http', netloc='example.com', path='/path/to/file.jpg', params='', query='param=1', fragment='')
print(parsed.path)
/path/to/file.jpg
=====================================================================================
parse_qs(), iter(), next()
parse_qs(): クエリ文字列(パラメータ)を辞書・リストに変換
iter() : 要素を一つずつ取り出す
next(): 次の要素を取得
from urllib.parse import parse_qs
qs = 'key1=value1&key2=value2%201&key2=value2%2F2'
qs_d = parse_qs(qs)
print(qs_d)
{'key1': ['value1'], 'key2': ['value2 1', 'value2/2']}
qs_d['key2']
['value2 1', 'value2/2']
next(iter(qs_d))
'key1'
next(iter(qs_d))
'key1'
mm = iter(qs_d)
next(mm)
'key1'
next(mm)
'key2'
=====================================================================================
self.path.endswith
HTTP サーバーでリクエストされた URL のパスが特定の拡張子で終わっているかどうかを確認する
from http.server import BaseHTTPRequestHandler, HTTPServer
class MyRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
# リクエストされたパスが '.css' で終わる場合...
if self.path.endswith('.css'):
# CSSファイルを処理するロジック
self.send_response(200)
self.send_header("Content-type", "text/css")
self.end_headers()
# ... ファイルを読み込んでクライアントに送信 ...
else:
# それ以外の場合(例: HTMLページなど)を処理するロジック
# ...
=====================================================================================
for
リスト,タプル
fruits = ['apple', 'banana', 'orange']
for fruit in fruits:
print(fruit)
apple
banana
orange
辞書
scores = {'English': 90, 'Math': 80, 'Science': 70}
for subject, score in scores.items():
print(f'{subject}: {score}')
English: 90
Math: 80
Science: 70
range関数
range(1, 10, 2): 1から9までの範囲の整数を2ずつ増加
for i in range(1, 10, 2):
print(i)
1
3
5
7
9
breakとcontinue
breakは、任意の部分でループから抜け出す
for i in range(5):
if i == 3:
break
print(i)
0
1
2
contiuneは残りの処理をスキップし、次のループを処理
for i in range(5):
if i == 3:
continue
print(i)
0
1
2
4
=====================================================================================
文字列を抽出
半角文字も全角文字も同じ1文字としてカウントされる。
インデックス
[]にインデックスを指定。インデックスは1文字目が0。
s = 'abcde'
print(s[0])
# a
print(s[4])
# e
負の値:後ろからの位置指定。-1が最後尾。
print(s[-1])
# e
print(s[-5])
# a
スライス
[start:stop] → start <= x < stopの範囲の文字列を抽出。
startを省略すると先頭から、stopを省略すると末尾までの範囲となる
s = 'abcde'
print(s[1:3])
# bc
print(s[:3])
# abc
print(s[1:])
# bcde
負の値も使える。
print(s[-4:-2])
# bc
print(s[:-2])
# abc
print(s[-4:])
# bcde
[start:stop:step] → 開始位置startと終了位置stopに加えて、増分step。
stepに負の値を指定すると後ろから順番に抽出される。
print(s[1:4:2])
# bd
print(s[::2])
# ace
print(s[::3])
# ad
print(s[::-1])
# edcba
print(s[::-2])
# eca
=====================================================================================
f文字列
Python 3.6から導入された強力な機能。文字列内に直接Python式を埋め込みコードの可読性と効率を大幅に向上。
変数の値を文字列に挿入
name = "太郎"
age = 30
print(f"私の名前は{name}で、{age}歳です。")
実行結果:
私の名前は太郎で、30歳です。
式の評価
x = 10
y = 20
print(f"{x} + {y} = {x + y}")
実行結果:
10 + 20 = 30
メソッドの呼び出し
name = "python"
print(f"大文字: {name.upper()}")
実行結果:
大文字: PYTHON
辞書のアクセス
person = {"name": "花子", "age": 25}
print(f"{person['name']}さんは{person['age']}歳です。")
実行結果:
花子さんは25歳です
条件式(三項演算子)
x = 15
print(f"{x}は{'偶数' if x % 2 == 0 else '奇数'}です。")
実行結果:
15は奇数です。
フォーマット指定子の使用
pi = 3.14159
print(f"円周率は{pi:.2f}です。")
実行結果:
円周率は3.14です
左寄せ、中央寄せ、右寄せ
name = "Python"
print(f"{name:<10}|") # 左寄せ
print(f"{name:^10}|") # 中央寄せ
print(f"{name:>10}|") # 右寄せ
実行結果:
Python |
Python |
Python|
日付のフォーマット
from datetime import datetime
now = datetime.now()
print(f"現在時刻: {now:%Y-%m-%d %H:%M:%S}")
実行結果(実行時の日時により異なります):
現在時刻: 2024-07-28 12:34:56
2進数、8進数、16進数表現
num = 42
print(f"10進数: {num}")
print(f"2進数: {num:b}")
print(f"8進数: {num:o}")
print(f"16進数: {num:x}")
実行結果:
10進数: 42
2進数: 101010
8進数: 52
16進数: 2a
デバッグ用の=指定子
x = 10
y = 20
print(f"{x=}, {y=}")
実行結果:
x=10, y=20
=====================================================================================
時刻取得
- 基本的な日時取得 →
datetime.now() - 時刻の計算や変換を楽にしたい →
arrow - タイムゾーンを考慮したい →
pytz - 処理時間計測 →
timeit.default_timer()ortime.perf_counter() - UNIXタイムが必要 →
time.time()
| 用途 | 適したライブラリ |
|---|---|
| 現在の日時を取得 (ローカルタイム) | datetime.datetime.now() |
| 現在の時刻をエポック秒で取得 | time.time() |
| 時刻を特定のフォーマットで表示 | time.localtime() + time.strftime() |
| タイムゾーンを考慮した時刻取得 | datetime.now(pytz.timezone(…)) or arrow.now() |
| WebAPIや時刻処理を簡単に扱いたい | arrow.now() |
| 時間計測 (コードの実行時間) | timeit.default_timer() or time.perf_counter() |
1. datetime.datetime.now()
用途・シチュエーション:
- 一般的な日時取得: 日付や時刻を扱うシンプルな処理に最適.
- フォーマットの自由度:
.strftime()を使ってフォーマットを変更しやすい. - ローカル時刻の取得:
datetime.now()はデフォルトでシステムのローカルタイムを取得する.
注意点:
- タイムゾーン情報は含まれないため,グローバルな処理には
pytzとの併用が必要.
from datetime import datetime
# 現在の日時を取得
time_datetime= datetime.now()
print(time_datetime)
# 2025-03-20 16:25:35.654095
2. time.time()
用途・シチュエーション:
- エポック秒(Unix時間)の取得: 1970年1月1日からの経過秒数で時刻を扱いたいときに最適.
- 時間差の計測: 2つの
time.time()の差を取ることで処理時間を計測できる.
注意点:
- 可読性が低いため,直接ユーザー向けに表示する用途には適さない.
import time
# 現在の時刻(エポック秒)を取得
time_time = time.time()
print(time_time)
print(f"{time_time:.3f}") # 小数点以下3桁まで表示
# 1742455535.6543741
# 1742455535.654
3. time.localtime() + time.strftime()
用途・シチュエーション:
- ローカル時刻を文字列として扱う:
strftime()によりフォーマットを簡単に変更できる. - ログ出力やUI表示:
YYYY-MM-DD HH:MM:SSのような形に整えやすい.
注意点:
datetimeに比べると直感的でない場合がある.
import time
# 現在の時刻をフォーマットして表示
local_time = time.localtime()
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
print(formatted_time)
# 2025-03-20 16:25:35
4. datetime.now(pytz.timezone("Asia/Tokyo"))
用途・シチュエーション:
- タイムゾーンを明示的に指定する必要がある場合: タイムゾーンが重要なアプリケーション(例: グローバル対応のWebサービス).
- 異なるタイムゾーン間での時刻変換:
pytz.timezone("UTC").localize(dt).astimezone(pytz.timezone("Asia/Tokyo"))などが可能.
注意点:
pytzは標準ライブラリではないため,pip install pytzが必要.
from datetime import datetime
import pytz
# タイムゾーンを指定して現在時刻を取得
time_pytz = pytz.timezone("Asia/Tokyo")
tokyo_time = datetime.now(time_pytz)
print(tokyo_time)
# 2025-03-20 16:25:35.943172+09:00
5. arrow.now()
用途・シチュエーション:
datetimeより直感的な時刻操作:.shift(days=1)で簡単に1日後の時刻を取得可能.- タイムゾーンを簡単に扱える:
.to('Asia/Tokyo')で変換できる. - WebアプリやAPIでの時刻処理: ISO 8601 形式 (
YYYY-MM-DDTHH:MM:SS+TZ) の出力が容易.
注意点:
arrowは標準ライブラリではなく,pip install arrowが必要.
import arrow
# 現在の時刻を取得
time_arrow= arrow.now()
print(time_arrow)
# 2025-03-20T16:25:36.045942+09:00
6. timeit.default_timer()
用途・シチュエーション:
- コードの実行時間計測:
start = timeit.default_timer(); do_something(); elapsed = timeit.default_timer() - start - 高精度な時間測定:
time.time()より精度が高い(環境によって異なる).
注意点:
datetimeやtimeとは異なり,時刻取得の用途には適さない.timeit.default_timer()は Windows ではtime.perf_counter(),Linux/macOS ではtime.monotonic()を内部的に使用する為,環境ごとに挙動が異なることがある.
import timeit
# 現在の時刻をエポック秒で取得
timestamp = timeit.default_timer()
print(timestamp)
# 15251.0218552
7. time.perf_counter()
用途・シチュエーション:
- 高精度な時間計測:
timeit.default_timer()同様,処理時間の測定に適している. - システムの時計と無関係に動作: システムの時刻とは無関係に動作する為,プロセスの実行時間の測定等に適しており,精度が高い.
注意点:
- 絶対的な時刻を取得するのではなく,相対時間の測定向け.
import time
# 高精度な時刻取得
current_time = time.perf_counter()
print(current_time)
# 15251.0220795
=====================================================================================
subprocess
外部処理をサブプロセスで行う。
subprocess.run()
- subprocess.run([“ls”])
- コマンドはリストで指定。
- シェルで実行するようなコマンドが実行できるが出力を取得出来ない。
- subprocess.run(“ls”,shell =True)
- パイプ(
|)、ワイルドカード(*)、環境変数展開、埋め込みコマンド(dir,cdなど)シェル機能が利用可能 - コマンドはスペース区切りの文字列で渡します
- パイプ(
- 戻り値
- proc = subprocess.run([“ls”],stdout = subprocess.PIPE, stderr = subprocess.PIPE)
- print(proc.stdout.decode(“utf8”)) // python側で出力を取る。
- cp = subprocess.run([‘ls’, ‘-a’])
- cp.returncode -> OK:1 NG: Other
- proc = subprocess.run([“ls”],stdout = subprocess.PIPE, stderr = subprocess.PIPE)
subprocess.Popen()
プロセスは並行に実行.起動したものが終了しているかどうかに関わらず,Popenの後に記述されたコードは実行
されます.
| 処理名 | 概要 |
|---|---|
| Popen() | プロセスの初期化、実行 |
| Popen.wait() | プロセスの終了を待機 プロセス終了後、終了コード(リターンコード)を返す |
| Popen.poll() | 非同期的にプロセスの出力を取得 |
| Popen.communicate() | 標準入力を渡し、標準出力と標準エラーを取得する |
| (stdout, stderr) | プロセスの標準出力や標準エラーを取得 |
Popen.wait()
# 'sleep 2' コマンドを実行するプロセスを起動
process = subprocess.Popen(['sleep', '2'])
print("プロセスを開始しました。")
# プロセスが終了するまで待機
return_code = process.wait()
print(f"プロセスが終了しました。終了コード: {return_code}")
Popen.poll()
import subprocess
import time
# プロセスを開始 (終了を待たない)
process = subprocess.Popen(['sleep', '5']) # 例: 5秒待機するコマンド
print("プロセスを開始しました。")
# プロセスが終了するまでポーリングしつつ、他の処理を行う
while True:
return_code = process.poll() # プロセスの状態をチェック
if return_code is not None:
print(f"プロセスが終了しました。終了コード: {return_code}")
break
else:
print("プロセスはまだ実行中です...")
time.sleep(1) # 1秒待ってから再度チェック
Popen.communicate()
import subprocess
import time
process = subprocess.Popen(
["grep", ".*\.txt"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
)
INPUT = "file_0.txt\nfile_1.txt\nfile_2.txt\nimg_1.jpg\nimg_2.jpg\nimg_3.jpg\n"
stdout, stderr = process.communicate(input=INPUT)
print("STDOUT:", stdout)
print("STDERR:", stderr)
=====================================================================================
リスト、タプル、辞書、集合
リスト [ ]
要素をカンマで区切って全体を大カッコ[]で囲む。
変更可。要素の挿入と削除を行うことができる。インデックス(番号)で要素にアクセス(読み書き)出来る。
List = ["A", "B", "C", "D", "E"]
print(List)
# ⇒ ['A', 'B', 'C', 'D', 'E']
List = ["A", "B", "C", "D", "E"]
print(List[2])
# ⇒ C
List = ["A", "B", "C", "D", "E"]
List[2]="Y"
List
# ⇒ ['A', 'B', 'Y', 'D', 'E']
タプル()
要素をカンマで区切って全体をカッコ()で囲む。->カッコ()無しでもOK
変更不可。インデックス(番号)で要素にアクセス(読み)出来る。
Tuple = ("A", "B", "C", "D", "E")
print(Tuple)
# ⇒ ('A', 'B', 'C', 'D', 'E')
Tuple = ("A", "B", "C", "D", "E")
print(Tuple[2])
# ⇒ C
Tuple = ("A", "B", "C", "D", "E")
Tuple[2]="Y"
Tuple
# ⇒ Error
# TypeError: 'tuple' object does not support item assignment
辞書 { }
key : valuのペアをカンマ区切って全体を中カッコ{ }で囲む。
変更可。リストに似ていますが要素へのアクセスはキーで行う。
Dictionary = {"USA":1, "JAPAN":2, "Germany":3}
Dictionary
# ⇒ {'USA': 1, 'JAPAN': 2, 'Germany': 3}
Dictionary = {"USA":1, "JAPAN":2, "Germany":3}
Dictionary["JAPAN"]
# ⇒ 2
集合 { }
set()関数を使うか、1個以上のカンマ区切りの値を波括弧で囲む。同じ要素を一つしか持てない。
変更可。要素へのアクセスはキーで行う。
SET = set(["USA","JAPAN","Germany","JAPAN"])
SET
# ⇒ {'Germany', 'JAPAN', 'USA'}
atomic = {'H','C','O','N'}
atomic
# ⇒ {'C', 'H', 'N', 'O'}
set("philadelphia")
# ⇒ {'a', 'd', 'e', 'h', 'i', 'l', 'p'}
リストから集合を作る
US_list = ["Philadelphia","New York","LA","Boston","New York"]
set(US_list)
# ⇒ {'Boston', 'LA', 'New York', 'Philadelphia'}
タプルから集合を作る
US_list2 = ("Philadelphia","New York","LA","Boston","New York")
set(US_list2)
# ⇒ {'Boston', 'LA', 'New York', 'Philadelphia'}
辞書をset()で囲むとキーだけが使われる
atomic_weight = {'H': 1.008, 'C': 12.01, 'O': 16, 'N': 14.01}
set(atomic_weight)
# ⇒ {'C', 'H', 'N', 'O'}
=====================================================================================
open()
open(file, mode=’r’, buffering=-1, encoding=None, errors=None, newline=None, closefd=True, opener=None)
- mode
文字 意味 'r'読み込み用に開く (デフォルト) 'w'書き込み用に開き、まずファイルを切り詰める 'x'排他的な生成に開き、ファイルが存在する場合は失敗する 'a'書き込み用に開き、ファイルが存在する場合には末尾に追記する 'b'バイナリモード 't'テキストモード (デフォルト) '+'更新用に開く (読み込み・書き込み用)
- buffering
- 0: バッファリングを無効化 (バイナリモードでのみ設定可能です)
- 1: 行単位でのバッファリング (テキストモードでの書き込み時のみ有効です)
- >1: 固定サイズのチャンクバッファに対するサイズをバイト単位で指定
- encoding
- テキストモードでのみ使用
- Pythonでサポートされているエンコーディングはどれでも使えます。
- errors
- エンコードやデコードでのエラーをどのように扱うかを指定
- バイナリモードでは使用できません
ファイル全体を文字列として読み込み: read()
with open(path) as f:
s = f.read()
print(s)
# line 1
# line 2
# line 3
ファイル全体をリストとして読み込み: readlines()
with open(path) as f:
l = f.readlines()
print(l)
# [‘line 1\n’, ‘line 2\n’, ‘line 3’]
ファイルを一行ずつ読み込み: readline()
with open(path) as f:
for s_line in f:
print(repr(s_line))
# ‘line 1\n’
# ‘line 2\n’
# ‘line 3’
文字列を書き込み: write()
path_w = ‘data/temp/test_w.txt’
s = ‘New file’
with open(path_w, mode=‘w’) as f:
f.write(s)
with open(path_w) as f:
print(f.read())
# New file
リストを書き込み: writelines()
l = [‘One’, ‘Two’, ‘Three’]
with open(path_w, mode=‘w’) as f:
f.writelines(l)
with open(path_w) as f:
print(f.read())
# OneTwoThree
リストの要素ごとに改行して書き込む(+演算子、joinなど)
with open(path_w, mode=‘w’) as f:
f.write(‘\n’.join(l))
with open(path_w) as f:
print(f.read())
# One
# Two
# Three
=====================================================================================