MathGroup Archive 1998

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Re: ReadList of Mixed Data Types???



>Todd A. Guillory wrote:
>>
>> I need to read this file into Mathematica:
>>
>> George Washington
>> 1732,2,22
>> 1777,12,15
>> 1778,1,20
>>
>> This is just a test, but the data file format HAS to be this way.   I
>> also have to use ReadList.  Don't ask!
>>
>> Anyway, I've tried several variations of ReadList with WordSeparator and
>> such NEVER get all the data, just parts.
>>
>> The first record is a string, the remaining three need to be 3-3 part
>> list of numbers.
>>
>> Any suggestions greatly appreciated.
>>
>> email tag@io.com
>
> "ToExpression" is also a technique I have used. In fact, I believe Todd
> could read the comma delimited data as Words using ReadList with
> WordSeparators->{","}, and then use ToExpression to convert the Words
> to integers.

Yes, this will work fine.  Here is one way to do it.  Variations of this
may be of interest depending on the format that you want to end up
with.

In[1]:= !!file
George Washington
1732,2,22
1777,12,15
1778,1,20

In[1]:= s = OpenRead["file"] ;

In[2]:= Read[s, String]

Out[2]= George Washington

In[3]:= Map[ToExpression,
          ReadList[s, Word, WordSeparators -> ",", RecordLists -> True],
{2}]

Out[3]= {{1732, 2, 22}, {1777, 12, 15}, {1778, 1, 20}}

In[4]:= Close[s] ;

> Unfortunately, this will not work for Reals expressed in Fortran
> E-format. The problem is that while ReadList very conveniently
> understands E-format, Mathematica in general does not, so neither does
> ToExpression. I very frequently use Mathematica to process DOS text
> format data files which have been stored by an HP semiconductor
> analyzer, which really only wants to record comma delimited data.

One common approach is to use a function like StringToNumber (defined
below) in place of ToExpression.  For example:

In[5]:= StringToNumber[p_] := Module[{s = StringToStream[p], result},
            result = Read[s, Number]; Close[s]; result]

In[6]:= !!file2
1.7e-3, 2.9E+17
2.4e12  0.003e8

In[6]:= Map[StringToNumber,
            ReadList["file2", Word, WordSeparators -> {" ", ","},
                         RecordLists -> True], {2}]

                        17          12 Out[6]= {{0.0017, 2.9 10  }, {2.4
10  , 300000.}}

There are also some interesting file-reading tools in the Experimental
Data Analyst application pack.  These tools can get quite
sophisticated.

If you have other questions about reading data into the Mathematica
kernel, and you would like to send them to me, I will be glad to take a
look at them.  If there is enough interest, I will prepare and
distribute a report about this.

Dave Withoff
Wolfram Research



  • Prev by Date: NotebookCovert
  • Next by Date: Re: what is wrong here?
  • Prev by thread: NotebookCovert
  • Next by thread: Re: Re: ReadList of Mixed Data Types???