Re: Import numbers and dates
- To: mathgroup at smc.vnet.net
- Subject: [mg30581] Re: [mg30558] Import numbers and dates
- From: "P.J. Hinton" <paulh at wolfram.com>
- Date: Thu, 30 Aug 2001 03:51:27 -0400 (EDT)
- Organization: "Wolfram Research, Inc."
- Sender: owner-wri-mathgroup at wolfram.com
On Wed, 29 Aug 2001, Martin Richter wrote:
> I want to import some data which consists 3862 lines, each lines looks like
> this:
> 07-01-85 101.101897 07-01-85 101.101897
>
> So 1. and 3. rows are dates and should be read that way and 2. and 4. rows
> are real numbers. I have tried something like this:
> data = Import["file.txt", "Table", DateStyle -> European];
There are several problems here:
1) The heuristic that Import[] applies to determine if a field is a date
assumes that a forward slash is used as a delimiter, not a minus sign.
Your example gets parsed as a subtraction operation.
2) Format specific settings for Import are passed as aggregate options.
The correct syntax for specifying the date format would be
ConversionOptions -> {"DateStyle" -> "European"}
3) The date uses a two-digit format. The default behavior is to leave
dates with two-digit years as strings rather than trying to expand them
out to a date with a four-digit year. There is a conversion option called
"TwoDigitYearFunction" which enables the user to specify what kind of
heuristic should be applied.
> Indeed this is easy in Excel but I would like to use Mathematica 4.1.
> (I properly getting environmental damage :-)
If you are dealing with a data set that cannot be reformatted easily, then
it's probably better to write a few lines of Mathematica code that is
tailored to your specific problem.
Start by writing a function that breaks a date field into its proper
elements:
parseMyDate[str_String]:=
Module[
{stmp, lst},
stmp = StringToStream[str];
lst = ReadList[stmp, {Word, Word, Word},
WordSeparators -> "-"];
Close[stmp];
Return[Flatten[ToExpression /@ lst]]
]
parseMyDate[expr_]:= expr
Note that this function should probably have some additional validation
elements if other, non-date, non-numeric fields are present.
We also provide a higher level function that reads in the complete file.
importMyTable[fileName_String]:=
Map[
parseMyDate,
ReadList[
fileName,
{Word, Number, Word, Number}
],
{2}
]
Here is the output for a two-record data set that uses your formatting
convention.
In[]:=
importMyTable["mydata.dat"]
Out[]=
{{{7, 1, 85}, 101.102, {7, 1, 85}, 101.102},
{{7, 1, 85}, 101.102, {7, 1, 85}, 101.102}}
--
P.J. Hinton
User Interface Programmer paulh at wolfram.com
Wolfram Research, Inc.