takecの気まぐれブログ

プログラミング等の話題を気まぐれに

PythonでCSVファイルを保存

numpyで計算した値をCSVファイルに保存したい場合 numpy.savetxt が便利 numpy.savetxt — NumPy v1.12 Manual

ただし、日本語等 latin-1 で表せない文字列は保存できない。

import numpy as np

N = 16
x = np.linspace(0,N,N,False)
y0 = x
y1 = x**2
y2 = np.sin(2*np.pi*x/N)
y3 = np.sin(2*np.pi*x/N)**2
savedata = np.c_[x,y0,y1,y2,y3]
print(savedata)

np.savetxt("data1.csv", savedata, fmt='%d,%e,%f,%+02.8f,%g')
np.savetxt("data2.csv",savedata,
    delimiter=',',
    newline='\r\n',
    header='x x x^2 sin(x) sin(x)^2')
0,0.000000e+00,0.000000,+0.00000000,0
1,1.000000e+00,1.000000,+0.38268343,0.146447
2,2.000000e+00,4.000000,+0.70710678,0.5
3,3.000000e+00,9.000000,+0.92387953,0.853553
4,4.000000e+00,16.000000,+1.00000000,1
5,5.000000e+00,25.000000,+0.92387953,0.853553
6,6.000000e+00,36.000000,+0.70710678,0.5
7,7.000000e+00,49.000000,+0.38268343,0.146447
8,8.000000e+00,64.000000,+0.00000000,1.49976e-32
9,9.000000e+00,81.000000,-0.38268343,0.146447
10,1.000000e+01,100.000000,-0.70710678,0.5
11,1.100000e+01,121.000000,-0.92387953,0.853553
12,1.200000e+01,144.000000,-1.00000000,1
13,1.300000e+01,169.000000,-0.92387953,0.853553
14,1.400000e+01,196.000000,-0.70710678,0.5
15,1.500000e+01,225.000000,-0.38268343,0.146447
# x x x^2 sin(x) sin(x)^2
0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00,0.000000000000000000e+00
1.000000000000000000e+00,1.000000000000000000e+00,1.000000000000000000e+00,3.826834323650897818e-01,1.464466094067262414e-01
2.000000000000000000e+00,2.000000000000000000e+00,4.000000000000000000e+00,7.071067811865475727e-01,5.000000000000001110e-01
3.000000000000000000e+00,3.000000000000000000e+00,9.000000000000000000e+00,9.238795325112867385e-01,8.535533905932737309e-01
4.000000000000000000e+00,4.000000000000000000e+00,1.600000000000000000e+01,1.000000000000000000e+00,1.000000000000000000e+00
5.000000000000000000e+00,5.000000000000000000e+00,2.500000000000000000e+01,9.238795325112867385e-01,8.535533905932737309e-01
6.000000000000000000e+00,6.000000000000000000e+00,3.600000000000000000e+01,7.071067811865475727e-01,5.000000000000001110e-01
7.000000000000000000e+00,7.000000000000000000e+00,4.900000000000000000e+01,3.826834323650898928e-01,1.464466094067263247e-01
8.000000000000000000e+00,8.000000000000000000e+00,6.400000000000000000e+01,1.224646799147353207e-16,1.499759782661857608e-32
9.000000000000000000e+00,9.000000000000000000e+00,8.100000000000000000e+01,-3.826834323650896152e-01,1.464466094067261304e-01
1.000000000000000000e+01,1.000000000000000000e+01,1.000000000000000000e+02,-7.071067811865474617e-01,4.999999999999998890e-01
1.100000000000000000e+01,1.100000000000000000e+01,1.210000000000000000e+02,-9.238795325112865164e-01,8.535533905932732868e-01
1.200000000000000000e+01,1.200000000000000000e+01,1.440000000000000000e+02,-1.000000000000000000e+00,1.000000000000000000e+00
1.300000000000000000e+01,1.300000000000000000e+01,1.690000000000000000e+02,-9.238795325112866275e-01,8.535533905932735088e-01
1.400000000000000000e+01,1.400000000000000000e+01,1.960000000000000000e+02,-7.071067811865476838e-01,5.000000000000002220e-01
1.500000000000000000e+01,1.500000000000000000e+01,2.250000000000000000e+02,-3.826834323650903924e-01,1.464466094067267132e-01