MathGroup Archive 2009

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

Search the Archive

Re: importing fixed width data

  • To: mathgroup at
  • Subject: [mg97597] Re: importing fixed width data
  • From: Szabolcs Horvát <szhorvat at>
  • Date: Mon, 16 Mar 2009 06:27:35 -0500 (EST)
  • References: <gpg1lt$cps$>

swiftset wrote:
> Hi, I'm trying to import data from a file where the fields are stored
> in a fixed width format. The Import["file", "Table"] command is the
> closest I can get to what I'd like, but then the data is divided using
> whitespace, so I get a lot of extraneous fields, and no way to tell
> whether what Mathematica imported as two fields is actually just one
> that contained a whitespace.

One possibility is to convert the file to a format that uses a comma as 
the delimiter (if your data has no commas in it), and then import as

Import["file", "CSV"]

The 'cut' command line program can do the conversion, and it can be used 
directly from Mathematica (if you have already have it installed and 
it's in the path).  Here's an example:

Import["!cut -c1-5,6-13,14- --output-delimiter=, file", "CSV"]

The -c1-5,6-13,14- part specifies the character ranges that should be 
taken as separate fields.

  • Prev by Date: Re: Don't understand the response
  • Next by Date: Re: Polygon with holes
  • Previous by thread: Re: importing fixed width data
  • Next by thread: Re: importing fixed width data