6
$\begingroup$

If I export

 x = {e, a, 5.*10^-15}
Export["testx.csv", x]

The result will be

"e"
"a"
5.000000000000001e-15

But I'd like the result to be

"e"
"a"
5.000000000000001E-15

Needs to export “.csv” unfortunately

$\endgroup$
1
  • $\begingroup$ does it have to be .csv? Could you use .m instead? $\endgroup$ – enano9314 Mar 8 at 0:51
10
$\begingroup$

If you want to temporarily modify export so that it uses "E", you can do the following:

Unprotect[Real];
Format[r_Real, CForm] /; $Real := Block[{$Real = False},
    Format[StringReplace["e" -> "E"] @ ToString[r, CForm], OutputForm]
]
Protect[Real];

Then:

Block[{$Real = True}, ExportString[{e,a,5.*^-15}, "CSV"]]
"\"e\"
\"a\"
5.E-15
"

If you want to make this more permanent, you could instead do:

Unprotect[Real];
FormatValues[Real] = {};
Format[r_Real,CForm] /; !TrueQ @ $Real := Block[{$Real = True},
    Format[StringReplace["e"->"E"]@ToString[r,CForm],OutputForm]
]
Protect[Real];

Then:

ExportString[{e,a,5.*^-15}, "CSV"]
"\"e\"
\"a\"
5.E-15
"
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.