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.

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="")
"u1","u2"
-5.9371883900074729e-01,1.4041106332197137e+00
3.8619059669064060e-01,-1.3181109089520018e+00
9.6132882982427406e-02,-7.5281659912741927e-01
2.5792642458131404e-01,1.9687596027732095e+00
-6.7129053308146580e-01,1.8557922404430598e+00

Read the previous CSV file.

sample = ot.Sample.ImportFromCSVFile("sample.csv", ",")
print(sample)
    [ u1         u2         ]
0 : [ -0.593719   1.40411   ]
1 : [  0.386191  -1.31811   ]
2 : [  0.0961329 -0.752817  ]
3 : [  0.257926   1.96876   ]
4 : [ -0.671291   1.85579   ]