how to save array to text file "correctly"?

115 views
Skip to first unread message

Hoang-Ngan Nguyen

unread,
Jun 23, 2016, 10:55:37 PM6/23/16
to julia-users
Hi,

I have the following array
data = [
 
-0.5
 
0.0
 
-2.18199e-5
 
1.53967e-5
 
-1.7899e-5
 
1.26717e-5
 
-2.24327e-6
 
1.60087e-6]

When I save it using either

writecsv("filename.csv",data)

or

writedlm("filename.csv",data,",")

I get this
-.5
0
-21819881018654233e-21
153966589305464e-19
-17898976869144106e-21
12671715235247999e-21
-22432716786997375e-22
16008706220269127e-22

Is there anyway for me to, instead, get the following:
-.5
0
-.000021819881018654233
.0000153966589305464
-.000017898976869144106
.000012671715235247999
-.0000022432716786997375
.0000016008706220269127

Thanks,
Ngan

Pieterjan Robbe

unread,
Jun 24, 2016, 5:50:56 AM6/24/16
to julia-users

f = open("myfile.csv","w")

for i in 1:length(data)

    write(f,@sprintf("%20.16f\n",data[i]))

end

close(f)


shell> cat myfile.csv

 -0.5000000000000000

  0.0000000000000000

 -0.0000218199000000

  0.0000153967000000

 -0.0000178990000000

  0.0000126717000000

 -0.0000022432700000

  0.0000016008700000


Op vrijdag 24 juni 2016 04:55:37 UTC+2 schreef Hoang-Ngan Nguyen:

Hoang-Ngan Nguyen

unread,
Jun 27, 2016, 2:41:50 AM6/27/16
to julia-users
Thanks,
Ngan
Reply all
Reply to author
Forward
0 new messages