Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

OutputForm[TableForm[data]]>>"test"

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?

share|improve this question
    
Why don't you simply use Export["test", data, "Table"] –  Algohi Jul 26 at 4:00
    
@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? –  matheorem Jul 26 at 6:38
    
@matheorem What do you mean it is "not enough sometimes"? Can you give an example? –  Mr.Wizard Jul 26 at 8:53
    
@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? –  matheorem Jul 27 at 11:58
    
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. –  Mr.Wizard Jul 27 at 12:16

1 Answer 1

up vote 3 down vote accepted

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

Edit

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

share|improve this answer
    
Wonderful solution! Thank you! –  matheorem Jul 26 at 6:26
    
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? –  matheorem Jul 26 at 6:41
    
This can be seen if you try real number like 0.1234567896 instead of integer. –  matheorem Jul 26 at 7:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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