MathGroup Archive 2008

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

Search the Archive

Re: file reading & processing

  • To: mathgroup at smc.vnet.net
  • Subject: [mg85092] Re: file reading & processing
  • From: Bill Rowe <readnewsciv at sbcglobal.net>
  • Date: Mon, 28 Jan 2008 03:26:45 -0500 (EST)

On 1/27/08 at 5:46 AM, TiborNyers at gmail.com (Tibor) wrote:

>Hi, my aim is to read a file in and than search it. A short part of
>the file:

>T,6,2,14,2
>T,21,1,15,1
>M,53,15,1,15
>T,62,14,16,14
>...
>I want to separate it by the first letter of the rows.

>Mathematica code I use:
>A = Import["location/file.txt", "Table"]
>tMat=Cases[A, {T, ___}]
>mMat=Cases[A, {M, ___}]

>The problem: Import generates a format that I cannot handle
>properly: List[List["T,6,2,14,2"],List["T,21,1,15,1"],List["M,
>53,15,1,15"],List["T,62,14,16,14"]]

This happens because you specified the Import format to be
"Table" with the default options. The default options set the
field separators to include spaces and tabs. Since neither are
present, Import sees each line of your file as a single string.

Probably the simplest fix is to specify the input format as
"CSV". The default options for this format set the field
separator to a comma which is matches the format you showed.
With that change, Cases will do what you want.

Another way to fix this would be to specify the field separators
for the "Table" format. That is add the directive
FieldSeparators->{","} after "Table". But since this effectively
makes the "Table" format the same as the "CSV" format, it seems
to me using "CSV" as the input format is simpler.
--
To reply via email subtract one hundred and four


  • Prev by Date: Text XY-size control in 2D graphics?
  • Next by Date: Re: Tick Mark Customization
  • Previous by thread: file reading & processing
  • Next by thread: Re: file reading & processing