0
$\begingroup$

I would like to format numbers of a list and write them in columns to file.

list = {{1, 2}, {3, 4}, {5, 6}};
dir = NotebookDirectory[];

Export[StringJoin[dir, "list1.dat"], list, "Table"];

The content of file list1.dat is (a single line):

1   2
3   4
5   6

When the numbers are formatted:

Export[StringJoin[dir, "list2.dat"], NumberForm[list, {5, 4}], "Table"];

The content of file list2.dat is:

{{1.0000, 2.0000}, {3.0000, 4.0000}, {5.0000, 6.0000}}

How can I get the content of list2.dat to be?:

1.0000 2.0000
3.0000 4.0000
5.0000 6.0000

UPDATE:

What would be the solution for:

list = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}}

to get with same formatting:

1.0000 2.0000 3.0000 4.0000
5.0000 6.0000 7.0000 8.0000
$\endgroup$
6
  • 1
    $\begingroup$ Does this work: Map[NumberForm[#, {5, 4}] &, list, {2}]? $\endgroup$
    – Kuba
    Commented May 31, 2016 at 11:40
  • $\begingroup$ Yes ... thank you ... this solves my problem $\endgroup$
    – mrz
    Commented May 31, 2016 at 11:43
  • $\begingroup$ @Kuba: Can you put this and if you have solution for the UPDATE to a answer, so that I can accept it as solution? $\endgroup$
    – mrz
    Commented May 31, 2016 at 12:31
  • $\begingroup$ Or mathematica.stackexchange.com/q/102846/5478 $\endgroup$
    – Kuba
    Commented May 31, 2016 at 12:34
  • 1
    $\begingroup$ Join @@@ {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}} first. $\endgroup$
    – Kuba
    Commented May 31, 2016 at 12:50

1 Answer 1

2
$\begingroup$

Given

data = {{{1, 2}, {3, 4}}, {{5, 6}, {7, 8}}};

then

formatted = Map[NumberForm[#, {5, 4}] &, ArrayReshape[data, {2, 4}], {-1}];
Export[FileNameJoin[{$HomeDirectory, "Desktop", "data.dat"}], formatted, "Table"];
FilePrint[FileNameJoin[{$HomeDirectory, "Desktop", "data.dat"}]]

prints

data

$\endgroup$

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