Re: Beginner--Exporting formatted text files
- To: mathgroup at smc.vnet.net
- Subject: [mg63867] Re: [mg63857] Beginner--Exporting formatted text files
- From: "David Annetts" <davidannetts at aapt.net.au>
- Date: Fri, 20 Jan 2006 04:32:21 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Hi,
> I am trying to create a formatted text file for input into
> another program. CSV or standard Table output will not work.
> I need columns at specific places in the file and for them
> to be right-aligned. The following prints to the screen the
> way I need it:
> PaddedForm[
> TableForm[data1,
> TableAlignments -> Right,
> TableHeadings -> {None, {"Col. 1;", "Col. 2;", "
> Col. 3;", "Col. 4;", "Col. 5;"}}], {6, 2}]
>
> I just can't figure out how to write it to a file so that
> this format is retained. I tried Export and Write but I lose
> the formatting. Please help before I become despondent and
> harm myself.
There are two ways I'd approach this problem. The first is to Export the
numbers from Mathematica, then write a small F95 program to format things
properly. However, since I don't have an F95 compiler ion my current
machine, I guess I'm down to only one way.
That way is to format your numbers, right-justified, 6 digits, 2 decimal
places as you want, then write them to a string. For example, if
data1 = Random[] & /@ Range[5];
then
ToString@PaddedForm[#, {6, 2}] & /@ data1
converts each entry to a right justified string, and
Apply[StringJoin, %]
will join them to a single string that you can
Write[file, OutputForm[%]];
For the record, there are various options to Export that should be able to
do what you need to do. However, I have never been able to get any of them
to work properly and I look forward to reading any other replies that may
throw some light on this. For example, if you could tell NumberForm how to
write numbers (eg 2 decimal places in a field of 6 digits), then you could
just use Export after
SetOptions[Export, ConversionOptions -> {
"ColumnAlignment" -> Right,
"FormatType" -> NumberForm}
];
But the best I can do is
SetOptions[NumberForm,
ExponentFunction -> (Null &)
];
Regards,
Dave.