Skip to main content
Updated to handle numbers in scientific notation
Source Link
Carl Woll
  • 131.7k
  • 6
  • 246
  • 359

Updated to handle numbers in scientific notation

You could block the formatting of Real objects:

Block[{Real},
    Format[r_Real, InputForm] := StandardForm @ NumberForm[rNumberForm[
        r, 
 3];       3,
        NumberFormat -> (Replace[#3, {"" -> #1, _ -> StringJoin[#1, "*^", #3]}]&)
    ];
    Put[someList, "tstfile.txt"]
]

Check:

Import["tstfile.txt"]

"{{{"a string"}, {1, 2}}, {{3, 4}, {E^(I/3), (-1)^(2/3)}}, {{5, 6}, {1.23 + 9.88I, -9.88 + 1.23I}}}"

You could block the formatting of Real objects:

Block[{Real},
    Format[r_Real, InputForm] := StandardForm @ NumberForm[r, 3];
    Put[someList, "tstfile.txt"]
]

Check:

Import["tstfile.txt"]

"{{{"a string"}, {1, 2}}, {{3, 4}, {E^(I/3), (-1)^(2/3)}}, {{5, 6}, {1.23 + 9.88I, -9.88 + 1.23I}}}"

Updated to handle numbers in scientific notation

You could block the formatting of Real objects:

Block[{Real},
    Format[r_Real, InputForm] := StandardForm @ NumberForm[
        r, 
        3,
        NumberFormat -> (Replace[#3, {"" -> #1, _ -> StringJoin[#1, "*^", #3]}]&)
    ];
    Put[someList, "tstfile.txt"]
]

Check:

Import["tstfile.txt"]

"{{{"a string"}, {1, 2}}, {{3, 4}, {E^(I/3), (-1)^(2/3)}}, {{5, 6}, {1.23 + 9.88I, -9.88 + 1.23I}}}"

Source Link
Carl Woll
  • 131.7k
  • 6
  • 246
  • 359

You could block the formatting of Real objects:

Block[{Real},
    Format[r_Real, InputForm] := StandardForm @ NumberForm[r, 3];
    Put[someList, "tstfile.txt"]
]

Check:

Import["tstfile.txt"]

"{{{"a string"}, {1, 2}}, {{3, 4}, {E^(I/3), (-1)^(2/3)}}, {{5, 6}, {1.23 + 9.88I, -9.88 + 1.23I}}}"