Delete spaces
Gary Herron
gherron at islandtraining.com
Fri Sep 28 12:01:21 EDT 2007
koutoo 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
>
>
After you recognize and handle the header and separator lines, the
remaining lines can be handled this way:
>>> l = '1 1 10:55:14 2 65 8.5'
>>> f = l.split()
>>> print f
['1', '1', '10:55:14', '2', '65', '8.5']
>>> ','.join(f)
'1,1,10:55:14,2,65,8.5'
or
>>> ', '.join(f)
'1, 1, 10:55:14, 2, 65, 8.5'
>>>
Gary Herron
More information about the Python-list
mailing list