8
$\begingroup$

As an example I have a data list of dimension {10, 5}:

data=Get["http://pastebin.com/raw/AxGXqgAb"]

{{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 want to export the integers (0-255) right aligned into a text file with 10 rows and 5 columns (no curly braces) and each integer should occupy 3 spaces with a white space in between.

My code does not exactly what I want and looks relatively complicated.

formatted=Map[NumberForm[#,{3,0},NumberPadding->{" ","0"},NumberPoint->""]&,data,{2}]

Export[StringJoin[imageDir,"\\timeStampBytes4.txt"],formatted,"Table"];

The output file contains:

  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

What is the most straightforward solution for my problem?

$\endgroup$

1 Answer 1

10
$\begingroup$
Export["file.txt", dat, "Table", Alignment -> Right]

"Table" lists this as an option. If you want more or specific space use "FieldSeparators":

Export["file.txt", dat, "Table", Alignment -> Right, "FieldSeparators" -> "    "]

FilePrint["file.txt"]
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
$\endgroup$
1
  • $\begingroup$ @mrz Glad I could help. :-) $\endgroup$
    – Mr.Wizard
    Commented Feb 24, 2017 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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