1
$\begingroup$

I have data like the following

dat = {{{0, 0, 311.429}, {0, 1, 0.0154873}}, {{1, 0, 0.0483966}, {1, 1, 0.0154212}}, {{2, 0, 0.0702652}, {2, 1, 0.0152236}}}

I want the data to be exported in text file in the following 3 column format

0  0   311.429
0  1   0.0154873
1  0   0.0483966
1  1   0.0154212
2  0   0.0702652
2  1   0.0152236 

Export["taba.txt", Flatten /@dat, "Table"]

But I did not get my desired format. How can I get my desired format?

$\endgroup$
3
  • 1
    $\begingroup$ "I did not get my desired format." - can you explain why the taba.txt produced by Export[] is not in a suitable format? $\endgroup$ Commented Mar 8, 2019 at 7:05
  • $\begingroup$ 'taba.txt'' is the filename where I want to export the data. $\endgroup$ Commented Mar 8, 2019 at 7:36
  • $\begingroup$ That is clear. So, when you opened taba.txt, why was it not in your desired format? $\endgroup$ Commented Mar 8, 2019 at 7:37

1 Answer 1

1
$\begingroup$

This will get you the file format looking for in your prolemstatement.

dat = {{{0, 0, 311.429}, {0, 1, 0.0154873}}, {{1, 0, 0.0483966}, {1, 
    1,  0.0154212}}, {{2, 0, 0.0702652}, {2, 1, 0.0152236}}}

dat1 = FlattenAt[dat, -1]

FlattenAt[dat1, 2]

FlattenAt[%, 1]

TableForm[{{0, 0, 311.429}, {0, 1, 0.0154873}, {1, 0, 0.0483966}, {1, 
   1, 0.0154212}, {2, 0, 0.0702652}, {2, 1, 0.0152236}}]

Export["taba.txt", %, "Table"]

SystemOpen["taba.txt"]

UPDATE:

But you may simply write

dat = {{{0, 0, 311.429}, {0, 1, 0.0154873}}, {{1, 0, 0.0483966}, {1, 
    1, 0.0154212}}, {{2, 0, 0.0702652}, {2, 1, 0.0152236}}}
Flatten[dat, 1]
Export["taba.txt", %, "Table"]
SystemOpen["taba.txt"]

For the second option , please give credit to @J. M. is computer-less

$\endgroup$
3
  • 2
    $\begingroup$ Why not just Flatten[dat, 1]? $\endgroup$ Commented Mar 8, 2019 at 7:53
  • $\begingroup$ @J.M.iscomputer-less You are very correct. My answer just give a broader sight of the use of these functions. $\endgroup$ Commented Mar 8, 2019 at 7:57
  • $\begingroup$ Thanks for the help. Methods are working perfectly $\endgroup$ Commented Mar 8, 2019 at 10:48

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.