Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2003
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2003

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

Search the Archive

Re: Re: Unformatted File IO

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44486] Re: [mg44446] Re: [mg44432] Unformatted File IO
  • From: Dr Bob <drbob at bigfoot.com>
  • Date: Wed, 12 Nov 2003 08:01:34 -0500 (EST)
  • Organization: Interstellar Corps of Engineers
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

This is ugly, but I think it works for the kind of input you're 
contemplating.

f = Switch[#,
         " ", If[inQuotes, #, ","],
         "\"", inQuotes = ! inQuotes; #,
         _, #] &;
g = StringReplace[#, ",," -> ","] &;
h = (inQuotes = False; ToExpression@g@StringJoin[f /@ Characters["{" <> # 
<>
     "}"]]) &;
handle = ToFileName["C:\Mathematica\Bruce", "Unformatted IO.txt"];
h /@ First@ReadList[handle, String,
         RecordLists -> True, RecordSeparators -> {"\n"}]

{{"hello there", 2.7},
   {1, 2, 3, 4}, {9, "abc",
    "def"}}

The g function can be omitted if you won't have consecutive spaces outside 
of quotes.

Bobby

Re: Unformatted File IO
Subject: [mg44486] [mg44446] Re: [mg44432] Unformatted File IO
From: "Bruce W. Colletti" <bcolletti at compuserve.com>
To: mathgroup at smc.vnet.net
References: <200311080951.EAA24713 at smc.vnet.net> 
<3FAD5923.2090409 at omrf.ouhsc.edu>

Yas

The code splits "hello there" which must stay intact as a list element.  
Any
ideas?  Thanks.

Bruce

----- Original Message -----
From: "Yasvir tesiram" <yat at omrf.ouhsc.edu>
To: mathgroup at smc.vnet.net
>
> The contents of test.txt are as you state below.
> SetDirectory["pathTotest"];
> /*check to see that the file is there*/
> FileNames[]
>
> (*open a file handle to the file (handle), read in the contents line by
> line (RecordSeparator) as words separated by any of the possible
> separators (WordSeparators) *)
>
> (*note the "\"" in the list of WordSeprators is intentional because of
> your double quoted strings*)
>
> handle = OpenRead["test.txt"];
> z = ToExpression[
>      ReadList[handle, Word, RecordSeparators -> {"\n"},
>        WordSeparators -> {" ", "\t", "\""}, RecordLists -> True]
>      ]
> Close[handle];
>
>
> Yas
>
> Bruce W. Colletti wrote:
>
> > I have an unformatted text file whose records hold reals and
double-quoted
> > strings, all delimited by spaces, e.g.,
> >
> >     "hello there"  2.7
> >     1 2 3 4
> >     9    "abc"      "def"
> >
> > How can I read the file to produce a list of lists, e.g., {{hello 
> there,
2.7},
> > {1,2,3,4}, {9, abc, def}}?


  • Prev by Date: Re: Unformatted File IO
  • Next by Date: Re: Re: Re: Re: Integration
  • Previous by thread: Re: Unformatted File IO
  • Next by thread: PlotLabel