Delete spaces

John Machin sjmachin at lexicon.net
Sat Sep 29 07:22:40 EDT 2007


On Sep 29, 1:43 am, kou... at hotmail.com wrote:
> If I have a text file that is delimited by spaces, how do I import it
> and get to comma delimited?  Here is a row of data from the text file:
>
> 1    1    10:55:14  2    65   8.5
> 1.4+1.1                       2.5       Class-2   0
>
> I tried a few examples from the group and it didn't work, since the
> file also has a header row and a row of seperators ( -------).  The
> lengths of each row is something like 130, so there are extra spaces
> after the last value as well.  I have tried joining and other things,
> but I couldn't figure out how to get the values to come together.
> Thanks.
>
> Kou

It would help enormously if you could show us UNAMBIGUOUSLY what is in
say the first 3 lines after the headings and separators -- do this:

print repr(open("thefile", "rb").read()[:400])

The other thing you need is to know enough about the file format to
show us what is the CSV output that you require from the sample input
-- we don't have crystal balls, and are likely to make half-donkeyed
guesses, like these:

If the spaces are really tabs, use line.split('\t')

Otherwise: the file has fixed column widths, and any use of line.split
will mangle it.

The clumsy way to handle this is to count column positions, and write
something ugly like:
field1 = line[0:8]
field2 = line[8:20]
etc

"a row of seperators ( -------)" sounds suspiciously like the "column
aligned" format that can be produced by running a SQL query on a SQL
Server database using MS's "Query Analyser". It looks like this:

RecordType  ID1                  ID2         Description
----------- -------------------- ----------- ----------------------
1           12345678             123456      Widget
4           87654321             654321      Gizmoid
etc

Does your file look something like that? If so, then all you have to
do is leverage off the fact that the second line has one-space gaps
between each bunch of dashes, and you can write a little module that
will read any file like that, just as though it were a CSV file.

Over to you ....

Cheers,
John




More information about the Python-list mailing list