ファイルの入出力〜私のためのpythonメモ

import os # この作業に必要なos
os.chdir('C:\\Users\\ryamada\\Desktop\\python') # Windowsでは'\'が'\\'になることに注意
  • 作業ディレクトリに出力する。np.savetxt()はアレイ的な形のものを書き出す
x = [1,2,3]
y = [5.,3,2]
z = [pi,2**5,.1]
np.savetxt('test.out', x, delimiter='\t')
np.savetxt('test2.out', (x,y,z),delimiter='\t')
  • 'test.out','test2.out'はそれぞれ以下のように(ワーキングディレクトリに)出る
1.000000000000000000e+00
2.000000000000000000e+00
3.000000000000000000e+00
1.000000000000000000e+00	2.000000000000000000e+00	3.000000000000000000e+00
5.000000000000000000e+00	3.000000000000000000e+00	2.000000000000000000e+00
3.141592653589793116e+00	3.200000000000000000e+01	1.000000000000000056e-01
data = np.loadtxt('populations.txt') # 読み込んで
year, hares, lynxes, carrots = data.T # 転置して行ごとに格納する
# プロットする
mp.pyplot.plot(year, hares, year, lynxes, year, carrots) # (year,hares),(year,lynxes)...
mp.pyplot.legend(('Hare', 'Lynx', 'Carrot'), loc=(1.05, 0.5))

  • 読み込んだファイルは、'#'始まりのヘッダー行を持つ、タブ区切り長方形レコード
# year	hare	lynx	carrot
1900	30e3	4e3	48300
1901	47.2e3	6.1e3	48200
1902	70.2e3	9.8e3	41500
1903	77.4e3	35.2e3	38200
1904	36.3e3	59.4e3	40600
1905	20.6e3	41.7e3	39800
1906	18.1e3	19e3	38600
1907	21.4e3	13e3	42300
1908	22e3	8.3e3	44500
1909	25.4e3	9.1e3	42100
1910	27.1e3	7.4e3	46000
1911	40.3e3	8e3	46800
1912	57e3	12.3e3	43800
1913	76.6e3	19.5e3	40900
1914	52.3e3	45.7e3	39400
1915	19.5e3	51.1e3	39000
1916	11.2e3	29.7e3	36700
1917	7.6e3	15.8e3	41800
1918	14.6e3	9.7e3	43300
1919	16.2e3	10.1e3	41300
1920	24.7e3	8.6e3	47300
  • 読み込まれたテキストファイルとその転置
data
Out[22]: 
array([[  1900.,  30000.,   4000.,  48300.],
       [  1901.,  47200.,   6100.,  48200.],
       [  1902.,  70200.,   9800.,  41500.],
       [  1903.,  77400.,  35200.,  38200.],
       [  1904.,  36300.,  59400.,  40600.],
       [  1905.,  20600.,  41700.,  39800.],
       [  1906.,  18100.,  19000.,  38600.],
       [  1907.,  21400.,  13000.,  42300.],
       [  1908.,  22000.,   8300.,  44500.],
       [  1909.,  25400.,   9100.,  42100.],
       [  1910.,  27100.,   7400.,  46000.],
       [  1911.,  40300.,   8000.,  46800.],
       [  1912.,  57000.,  12300.,  43800.],
       [  1913.,  76600.,  19500.,  40900.],
       [  1914.,  52300.,  45700.,  39400.],
       [  1915.,  19500.,  51100.,  39000.],
       [  1916.,  11200.,  29700.,  36700.],
       [  1917.,   7600.,  15800.,  41800.],
       [  1918.,  14600.,   9700.,  43300.],
       [  1919.,  16200.,  10100.,  41300.],
       [  1920.,  24700.,   8600.,  47300.]])

data.T
Out[23]: 
array([[  1900.,   1901.,   1902.,   1903.,   1904.,   1905.,   1906.,
          1907.,   1908.,   1909.,   1910.,   1911.,   1912.,   1913.,
          1914.,   1915.,   1916.,   1917.,   1918.,   1919.,   1920.],
       [ 30000.,  47200.,  70200.,  77400.,  36300.,  20600.,  18100.,
         21400.,  22000.,  25400.,  27100.,  40300.,  57000.,  76600.,
         52300.,  19500.,  11200.,   7600.,  14600.,  16200.,  24700.],
       [  4000.,   6100.,   9800.,  35200.,  59400.,  41700.,  19000.,
         13000.,   8300.,   9100.,   7400.,   8000.,  12300.,  19500.,
         45700.,  51100.,  29700.,  15800.,   9700.,  10100.,   8600.],
       [ 48300.,  48200.,  41500.,  38200.,  40600.,  39800.,  38600.,
         42300.,  44500.,  42100.,  46000.,  46800.,  43800.,  40900.,
         39400.,  39000.,  36700.,  41800.,  43300.,  41300.,  47300.]])
  • グラフを出力する・pngファイルを読み込む
mp.pyplot.plot(year, hares, year, lynxes, year, carrots)
mp.pyplot.legend(('Hare', 'Lynx', 'Carrot'), loc=(1.05, 0.5))
mp.pyplot.savefig("graph.png")
    • 上2行を実行し、Ipythonのコンソールに現れるプロットを右クリックして画像保存してもよいが、上の第3行で保存したほうがファイルサイズが大きい
    • ここで作った"graph.png"を読み込む。3次元アレイになっていることがわかる
img_read = mp.pyplot.imread('graph.png')
img_read.shape
Out[52]: (288, 432, 4)
    • ちなみに、画像を扱うならこちらを参考