I have a mathematica script computing a list of numbers to high precision (5000 digits) which I would like to input into another program. I am running mathematica from the terminal to generate the file.

If I just ask for the output directly, the file it creates has a lot of superfluous return characters and ">" characters which would be very painful to remove.

If I ask for the output in "InputForm" it is cleaner, but it includes information about the precision which is also painful to remove.

Is there a way to generate the output in a clean way for other uses? Here I simply want to generate a list of numbers (comma separated is fine) in decimal notation. Note that I have to run this in a terminal window since it is a long computation on a remove machine, not to mention that the output is extremely long.

Here is an example file (foo) and example output (foo.out) generated by (math < foo > foo.out)


Table[N[y[n], 200], {n, 1, 3}];
InputForm[Table[N[y[n], 200], {n, 1, 3}]]


In[2]:= In[2]:= 
Out[2]= {0.585786437626904951198311275790301921430328124623051926823320262009\
>     26752153789296114961246567235842726498615376908770297507516394414926278\
>     735587850290006416858677733407249440724420004949884721793942853, 
>    0.4309644062711508251997185459650503202383880207705086544705533770015445\
>     86922982160191602077612059737877497692294847950495845860657358210464559\
>     31308381667736143112955567874906787403334158314120298990475, 
>    0.3292893218813452475599155637895150960715164062311525963411660131004633\
>     76076894648057480623283617921363249307688454385148753758197207463139367\
>     79392514500320842933886670362472036221000247494236089697143}

In[3]:= In[3]:= 
6416858677733407249440724420004949884721793942852989044002839`200., 0.4309644\
955567874906787403334158314120298990475498174000474`200., 0.32928932188134524\


I tried Daniel Huber's suggestion, but the output file I got was different (Full disclosure, I used Export["test.txt", d] instead. I am using Mathematica 12.3.1 Kernel for Linux x86 (64-bit).

  • $\begingroup$ Modern approach to this would probably be to use the wolframscript command and ExportString format of your choosing. For instance, wolframscript -code 'ExportString[N[Pi, 5000], "CSV"]' > foo.out or with the corresponding code in file foo with wolframscript -file foo > foo.out $\endgroup$
    – kirma
    Commented Aug 14, 2022 at 17:29
  • $\begingroup$ Some methods to process your output here: How to format a large Number $\endgroup$ Commented Aug 15, 2022 at 6:43

1 Answer 1


Here is a simple example. I am using a list with 3 elements and I output 100 digits to a file:

d = N[{Pi, E, Log[Pi]}, 100];
Export["d:/tmp/test.txt", ToString@d]

The file looks like:

enter image description here

To reimport it into MMA:

Import["d:/tmp/test.txt", "List"][[1]] // ToExpression

and you will get a list of the original numbers.

  • $\begingroup$ Thanks! Unfortunately I'm still getting extra characters which record the number of digits of accuracy (I edited my question since when I tried to write the output as a comment it was a mess) $\endgroup$
    – user297024
    Commented Aug 15, 2022 at 2:44
  • $\begingroup$ Add "ToString", that should fix it. I added it to my answer. $\endgroup$ Commented Aug 15, 2022 at 6:25
  • $\begingroup$ Thanks, that worked! $\endgroup$
    – user297024
    Commented Aug 15, 2022 at 12:24
  • $\begingroup$ Ugh, 24 hours of computation and I get I mess. These numbers are smallish (less than $1$ but bigger than $10^{-100}$ but if you type: d:=N[Pi/10^100,100]; and then ToString@d you get a mess. $\endgroup$
    – user297024
    Commented Aug 16, 2022 at 18:21
  • $\begingroup$ "ToSTring" only works o.k. for numbers without exponents. For small numbers that needs exponents we need a format that displays the exponent on the same line, that is 1D. You may e.g. use "FortranForm" or "CForm" for this purpose. Or you can define a format yourself like e.g.: NumberForm[data, NumberFormat -> (Row[{#1, "^", #3}] &)] $\endgroup$ Commented Aug 16, 2022 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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