0

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
mrz
  • 11,686
  • 2
  • 25
  • 81

1 Answers1

2

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

m_goldberg
  • 107,779
  • 16
  • 103
  • 257