# Write formatted data line by line in a loop into a text file

The following question is related to this one.

I have as example this list:

{{15, 0, 0, 69, 250}, {15, 0, 0, 88, 121}, {15, 0, 0, 107, 248},
{15, 0, 0, 125, 119}, {15, 0, 0, 144, 246}, {15, 0, 0, 163, 117},
{15, 0, 0, 181, 244}, {15, 0, 0, 200, 115}, {15, 0, 0, 219, 242},
{15, 0, 0, 237, 113}};


I would like to open a file for writing and then write the data right aligned line by line in a loop (not at once with Export) to the file. Finally the file should be closed.

The content of the file should be:

15    0    0     69    250
15    0    0     88    121
15    0    0    107    248
15    0    0    125    119
15    0    0    144    246
15    0    0    163    117
15    0    0    181    244
15    0    0    200    115
15    0    0    219    242
15    0    0    237    113


I tried the following:

list = {{15, 0, 0, 69, 250}, {15, 0, 0, 88, 121}, {15, 0, 0, 107, 248},
{15, 0, 0, 125, 119}, {15, 0, 0, 144, 246}, {15, 0, 0, 163, 117},
{15, 0, 0, 181, 244}, {15, 0, 0, 200, 115}, {15, 0, 0, 219, 242},
{15, 0, 0, 237, 113}};

n = Length[list];

file = OpenWrite["d:\\output.txt"];

Do[
Write[file, list[[i]]];
, {i, 1, n}
];

Close[file];


The text content is not right aligned and containes curly braces and commas:

{15, 0, 0, 69, 250}
{15, 0, 0, 88, 121}
{15, 0, 0, 107, 248}
{15, 0, 0, 125, 119}
{15, 0, 0, 144, 246}
{15, 0, 0, 163, 117}
{15, 0, 0, 181, 244}
{15, 0, 0, 200, 115}
{15, 0, 0, 219, 242}
{15, 0, 0, 237, 113}

• If you're willing to preprocess with a combination of StringPadLeft and StringJoin to construct each line you could do this relatively painlessly with WriteString. Mar 14 '17 at 15:02
– mrz
Mar 14 '17 at 15:06
• Mar 14 '17 at 15:07
• You can write to a stream using Export. Or you can ExportString, then WriteString the result to the stream. The purpose of Export/ExportString here is to create the desired formatting. Mar 14 '17 at 15:09
• @Szabolcs I think it depends on the formatting desired. Since the OP wants to do it line-by-line if a tab would suffice ExportString[ line, "TSV" ] should work, but otherwise the justification needs to be done by hand I think. Mar 14 '17 at 15:14

So depending on whether tab separation is sufficient or not there are two approaches.

First is the basic string-it-all-together approach:

With[{file =
OpenWrite@FileNameJoin@{$TemporaryDirectory, "output.txt"}}, Do[ WriteLine[ file, StringPadLeft[ToString@#, 6] & /@ d // StringTrim@*StringJoin ], {d, data} ]; Close@file ] // Import  Alternatively if you can deal with tabs as your separators, not enforcing some width-convention (I think this is probably the more common case): With[{file = OpenWrite@FileNameJoin@{$TemporaryDirectory, "output.txt"}},

Do[
WriteLine[file,
ExportString[
{d},
"TSV"
]
],
{d, data}
];

Close@file
]


I would generally prefer the second, myself, as the more I can stick with what WRI provides the easier my life tends to be.

• Great. Is it possible to align the numbers to right in each column?
– mrz
Mar 14 '17 at 15:26
• Yeah, replace StringPadRight with StringPadLeft should do it. And then put StringTrim@* in front of StringJoin. Mar 14 '17 at 15:27
• @mrz edited the answer to reflect that. Mar 14 '17 at 15:29
• The first code works great. The second is left aligned.
– mrz
Mar 14 '17 at 15:39
• @mrz I didn't change the second at all. Not sure there's a way to do left-aligned with tabs without specifying that at the text-editor level or something. Mar 14 '17 at 15:44

Another possibility:

list = {{15, 0, 0, 69, 250}, {15, 0, 0, 88, 121}, {15, 0, 0, 107, 248},
{15, 0, 0, 125, 119}, {15, 0, 0, 144, 246}, {15, 0, 0, 163, 117},
{15, 0, 0, 181, 244}, {15, 0, 0, 200, 115}, {15, 0, 0, 219, 242},
{15, 0, 0, 237, 113}};

n = Length[list];

file = OpenWrite@FileNameJoin@{\$TemporaryDirectory, "output.txt"};

Do[

outputline = StringJoin[
ToString[
]
];

WriteLine[file, outputline];

, {i, 1, n}

];

Close[file] // Import

15   0   0  69 250
15   0   0  88 121
15   0   0 107 248
15   0   0 125 119
15   0   0 144 246
15   0   0 163 117
15   0   0 181 244
15   0   0 200 115
15   0   0 219 242
15   0   0 237 113

• There's also another form that routes through ExportString but I forgot to add it to my answer. Mar 15 '17 at 21:06