「テキストファイルの入出力」の編集履歴(バックアップ)一覧はこちら
テキストファイルの入出力 - (2025/03/10 (月) 23:43:45) の1つ前との変更点
追加された行は緑色になります。
削除された行は赤色になります。
** 目次
#contents
** フィールドが複数の空白で区切られているテキストファイルを簡単に読み込む
pandasのread_csv関数を使う。「Pythonで動かして学ぶ!あたらしい機械学習の教科書 第2版」の図5.1に掲載されているデータで試してみる。そのデータは以下のとおり。
#highlight(){{
15.43 170.91
23.01 160.68
5.00 129.00
12.56 159.70
8.67 155.46
7.31 140.56
9.66 153.65
13.64 159.43
14.92 164.70
18.47 169.65
15.48 160.71
22.13 173.29
10.11 159.31
26.95 171.52
5.68 138.96
21.76 165.87
}}
これをファイルch5_data.txtに保存しておく。
#highlight(){{
>>> import pandas as pd
>>> df = pd.read_csv('ch5_data.txt', header = None, sep = '\s+')
>>> df
0 1
0 15.43 170.91
1 23.01 160.68
2 5.00 129.00
3 12.56 159.70
4 8.67 155.46
5 7.31 140.56
6 9.66 153.65
7 13.64 159.43
8 14.92 164.70
9 18.47 169.65
10 15.48 160.71
11 22.13 173.29
12 10.11 159.31
13 26.95 171.52
14 5.68 138.96
15 21.76 165.87
}
読み込まれたデータはpandasのDataFrame型(データフレーム)。これをNumPyのndarray型(行列)に変換するにはto_numpy関数を使う。
#highlight(){{
>>> d = df.to_numpy()
>>> d
array([[ 15.43, 170.91],
[ 23.01, 160.68],
[ 5. , 129. ],
[ 12.56, 159.7 ],
[ 8.67, 155.46],
[ 7.31, 140.56],
[ 9.66, 153.65],
[ 13.64, 159.43],
[ 14.92, 164.7 ],
[ 18.47, 169.65],
[ 15.48, 160.71],
[ 22.13, 173.29],
[ 10.11, 159.31],
[ 26.95, 171.52],
[ 5.68, 138.96],
[ 21.76, 165.87]])
}}
NumPyの関数を試してみる。以下は1列目と2列目の最大値をそれぞれ抜き出した例。
#highlight(){{
>>> np.max(d[:, 0])
26.95
>>> np.max(d[:, 1])
173.29
}}
** ndarray型の中身を簡単にテキストファイルに出力する
savetxt関数を使う。
#highlight(){{
>>> import numpy as np
>>> mx = np.array([[1, 2, 3], [4, 5, 6]])
>>> np.savetxt('out.txt', mx, fmt = '%.3f')
}}
出力したファイルout.txtは、以下のように出力されているはず。
#highlight(){{
1.000 2.000 3.000
4.000 5.000 6.000
}}
** ファイルやディレクトリの存在を確認する
osモジュールのpath.exists関数を使う。path.exists関数は引数に与えたパスがファイルでもディレクトリでも存在していればTrueを返す。
ファイルかディレクトリの判定はpath.isfile関数かpath.isdir関数を使う。この両関数は、存在しないパスを指定するとFalseを返す。
#highlight(){{
>>> import os
>>> os.path.exists('C:\Windows\write.exe')
True
>>> os.path.exists('C:\Windows')
True
>>> os.path.exists('C:\Windows\write.ex')
False
>>> os.path.exists('C:\Win')
False
>>> os.path.isfile('C:\Windows\write.exe')
True
>>> os.path.isdir('C:\Windows\write.exe')
False
>>> os.path.isfile('C:\Windows')
False
>>> os.path.isdir('C:\Windows')
True
>>> os.path.isfile('C:\Win')
False
>>> os.path.isdir('C:\Win')
False
}}
** パスセパレーターを得る
osモジュールのsepを使う。以下は、ウィンドウズで実行した例。
#highlight(){{
>>> import os
>>> print(os.sep)
\
>>> print('csv' + os.sep + 'result.csv')
csv\result.csv
}}
** 目次
#contents
** フィールドが複数の空白で区切られているテキストファイルを簡単に読み込む
pandasのread_csv関数を使う。「Pythonで動かして学ぶ!あたらしい機械学習の教科書 第2版」の図5.1に掲載されているデータで試してみる。そのデータは以下のとおり。
#highlight(){{
15.43 170.91
23.01 160.68
5.00 129.00
12.56 159.70
8.67 155.46
7.31 140.56
9.66 153.65
13.64 159.43
14.92 164.70
18.47 169.65
15.48 160.71
22.13 173.29
10.11 159.31
26.95 171.52
5.68 138.96
21.76 165.87
}}
これをファイルch5_data.txtに保存しておく。
#highlight(){{
>>> import pandas as pd
>>> df = pd.read_csv('ch5_data.txt', header = None, sep = '\s+')
>>> df
0 1
0 15.43 170.91
1 23.01 160.68
2 5.00 129.00
3 12.56 159.70
4 8.67 155.46
5 7.31 140.56
6 9.66 153.65
7 13.64 159.43
8 14.92 164.70
9 18.47 169.65
10 15.48 160.71
11 22.13 173.29
12 10.11 159.31
13 26.95 171.52
14 5.68 138.96
15 21.76 165.87
}}
読み込まれたデータはpandasのDataFrame型(データフレーム)。これをNumPyのndarray型(行列)に変換するにはto_numpy関数を使う。
#highlight(){{
>>> d = df.to_numpy()
>>> d
array([[ 15.43, 170.91],
[ 23.01, 160.68],
[ 5. , 129. ],
[ 12.56, 159.7 ],
[ 8.67, 155.46],
[ 7.31, 140.56],
[ 9.66, 153.65],
[ 13.64, 159.43],
[ 14.92, 164.7 ],
[ 18.47, 169.65],
[ 15.48, 160.71],
[ 22.13, 173.29],
[ 10.11, 159.31],
[ 26.95, 171.52],
[ 5.68, 138.96],
[ 21.76, 165.87]])
}}
NumPyの関数を試してみる。以下は1列目と2列目の最大値をそれぞれ抜き出した例。
#highlight(){{
>>> np.max(d[:, 0])
26.95
>>> np.max(d[:, 1])
173.29
}}
** ndarray型の中身を簡単にテキストファイルに出力する
savetxt関数を使う。
#highlight(){{
>>> import numpy as np
>>> mx = np.array([[1, 2, 3], [4, 5, 6]])
>>> np.savetxt('out.txt', mx, fmt = '%.3f')
}}
出力したファイルout.txtは、以下のように出力されているはず。
#highlight(){{
1.000 2.000 3.000
4.000 5.000 6.000
}}
** ファイルやディレクトリの存在を確認する
osモジュールのpath.exists関数を使う。path.exists関数は引数に与えたパスがファイルでもディレクトリでも存在していればTrueを返す。
ファイルかディレクトリの判定はpath.isfile関数かpath.isdir関数を使う。この両関数は、存在しないパスを指定するとFalseを返す。
#highlight(){{
>>> import os
>>> os.path.exists('C:\Windows\write.exe')
True
>>> os.path.exists('C:\Windows')
True
>>> os.path.exists('C:\Windows\write.ex')
False
>>> os.path.exists('C:\Win')
False
>>> os.path.isfile('C:\Windows\write.exe')
True
>>> os.path.isdir('C:\Windows\write.exe')
False
>>> os.path.isfile('C:\Windows')
False
>>> os.path.isdir('C:\Windows')
True
>>> os.path.isfile('C:\Win')
False
>>> os.path.isdir('C:\Win')
False
}}
** パスセパレーターを得る
osモジュールのsepを使う。以下は、ウィンドウズで実行した例。
#highlight(){{
>>> import os
>>> print(os.sep)
\
>>> print('csv' + os.sep + 'result.csv')
csv\result.csv
}}
表示オプション
横に並べて表示:
変化行の前後のみ表示: