Mathematica 9 is now available
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: Unformatted File IO

  • To: mathgroup at smc.vnet.net
  • Subject: [mg44481] Re: [mg44432] Unformatted File IO
  • From: Yasvir Tesiram <yat at omrf.ouhsc.edu>
  • Date: Wed, 12 Nov 2003 08:01:30 -0500 (EST)
  • References: <200311080951.EAA24713@smc.vnet.net> <3FAD5923.2090409@omrf.ouhsc.edu> <000801c3a6e3$e83fd3f0$6401a8c0@bcolletti>
  • Reply-to: yat at omrf.ouhsc.edu
  • Sender: owner-wri-mathgroup at wolfram.com

Using Record instead of Word and then doing a string replacement on the 
first Record in the List.

handle = OpenRead["test.txt"];
z = ReadList[handle, Record,
     RecordSeparators -> {"\n"},
     RecordLists -> True]
Close[handle];

StringReplace[z[[1]], {"\" " -> ", ", "\"" -> ""}]

But you would have to make a pattern matcher and replacer for all your 
different types of lines if you use Record. For example, z[[2]] is 
separated by whitespace rather than commas.

Alternatively you can use the construct below or Jens-Peer Kuskas 
suggestion of Import["test.txt","Table"] where the StringReplace rules 
may be easier to apply.

Theres got to be a more hassle free way. I just can't think of one off hand.

Cheers
Yas


Bruce W. Colletti wrote:
> 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: Linear integral along a given curve.
  • Next by Date: Re: Re: Unformatted File IO
  • Previous by thread: Re: Unformatted File IO
  • Next by thread: Re: Unformatted File IO