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.2153343201919318e-01,-2.4122348956683715e-01
-1.7879641991225605e+00,4.0135974377270112e-01
1.3678255231152090e+00,1.0043431266393292e+00
7.4154838407726686e-01,-4.3612337965035816e-02
5.3934467806783382e-01,2.9995041327320021e-01

read the previous CSV file

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

Out:

    [ u1         u2         ]
0 : [ -0.721533  -0.241223  ]
1 : [ -1.78796    0.40136   ]
2 : [  1.36783    1.00434   ]
3 : [  0.741548  -0.0436123 ]
4 : [  0.539345   0.29995   ]

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

Gallery generated by Sphinx-Gallery