Now I have a list stored in data as follows

data = {{"Ca", 1, 1, 1}, {"Ca", 2, 2, 2}, {"Ca", 3, 3, 3}}

the TableForm of data is of course as follows:

Ca  1   1   1
Ca  2   2   2
Ca  3   3   3

But how to write this TableForm of data into a file?

I tried code like


But the result is

Ca   1   1   1

Ca   2   2   2

Ca   3   3   3

Why there are blank lines???? How to prevent the emergence of these blank lines when write TableForm expression into a file?

  • $\begingroup$ Why don't you simply use Export["test", data, "Table"] $\endgroup$ – Algohi Jul 26 '14 at 4:00
  • $\begingroup$ @Algohi Thank you! I think Lou's answer which is to control TableSpacing is more flexible, for example in OpenWrite and Write cases? But there is a problem, the accuracy of the number that OutputForm gives is not enough sometimes, do you know other method to solve this? $\endgroup$ – matheorem Jul 26 '14 at 6:38
  • $\begingroup$ @matheorem What do you mean it is "not enough sometimes"? Can you give an example? $\endgroup$ – Mr.Wizard Jul 26 '14 at 8:53
  • $\begingroup$ @Mr.Wizard Thank you, Mr. Wizard. I mean if data = {{"Ca", 1.123456789, 1.98765432}}, then OutputForm[TableForm[data, TableSpacing -> {0, 3}]] >> "test" will give Ca 1.12346 1.98765, you can see the accuracy is downgrade. But Lou gives addtional answer that suggest using SetAccuracy, it works well. What do you think of Lou's method? $\endgroup$ – matheorem Jul 27 '14 at 11:58
  • $\begingroup$ It looks fine to me, and since that addition addresses your concern I'd say it earns Acceptance. I haven't benchmarked that code however and one lingering concern would be that it might not perform as well as a manual approach using WriteString. $\endgroup$ – Mr.Wizard Jul 27 '14 at 12:16

In this case, it is advisable to use the Export command:

Export["test", data, "Table"]

Leads to:

enter image description here

Alternative one can use TableSpacing i.e. manipulate space between rows or columns,

OutputForm[TableForm[data, TableSpacing -> {0, 0}]] >> "test"

this leads to;

enter image description here


One can control Accuracy by:

data1 = SetAccuracy[RandomReal[{-1, 1}, {3, 4}], 4]

{{-0.630, -0.379, -0.717, -0.968}, {-0.497, -0.609, 0.461, -0.121}, {-0.463, -0.303, -0.574, 0.527}}

OutputForm[TableForm[data1, TableSpacing -> {0, 2}]] >> "uups"

yields to:

enter image description here

  • $\begingroup$ Wonderful solution! Thank you! $\endgroup$ – matheorem Jul 26 '14 at 6:26
  • $\begingroup$ As I metioned in the reply to Algohi. I just found that the accuracy of numbers that OutputForm gives is much lower than Export gives. But Export seems not univeral in file operation like OpenWrite and Write, so is there a better solution? $\endgroup$ – matheorem Jul 26 '14 at 6:41
  • $\begingroup$ This can be seen if you try real number like 0.1234567896 instead of integer. $\endgroup$ – matheorem Jul 26 '14 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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