Re: importing fixed width data
- To: mathgroup at smc.vnet.net
- Subject: [mg97597] Re: importing fixed width data
- From: Szabolcs Horvát <szhorvat at gmail.com>
- Date: Mon, 16 Mar 2009 06:27:35 -0500 (EST)
- References: <firstname.lastname@example.org>
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.