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}
StringPadLeft
andStringJoin
to construct each line you could do this relatively painlessly withWriteString
. $\endgroup$ExportString[ line, "TSV" ]
should work, but otherwise the justification needs to be done by hand I think. $\endgroup$