Import / export a sample via a CSV file

In this example we are going to import and export a data sample from/to a CSV file.

from __future__ import print_function
import openturns as ot
ot.Log.Show(ot.Log.NONE)

create a sample from a 2-d gaussian distribution

sample = ot.Normal(2).getSample(5)
sample.setDescription(['u1', 'u2'])

write a CSV file warning the default separator is ‘;’

sample.exportToCSVFile('sample.csv', ',')

print the content of the written file

with open('sample.csv', 'r') as f:
    for line in f.readlines():
        print(line, end='')

Out:

"u1","u2"
7.4154838407726686e-01,-4.3612337965035816e-02
5.3934467806783382e-01,2.9995041327320021e-01
4.0771715787264406e-01,-4.8511195560000947e-01
-3.8299201615804984e-01,-7.5281659912741927e-01
2.5792642458131404e-01,1.9687596027732095e+00

read the previous CSV file

sample = ot.Sample.ImportFromCSVFile('sample.csv', ',')
print(sample)

Out:

    [ u1         u2         ]
0 : [  0.741548  -0.0436123 ]
1 : [  0.539345   0.29995   ]
2 : [  0.407717  -0.485112  ]
3 : [ -0.382992  -0.752817  ]
4 : [  0.257926   1.96876   ]

Total running time of the script: ( 0 minutes 0.005 seconds)

Gallery generated by Sphinx-Gallery