Reading a file, sans whitespace
Rick L. Ratzel
rick.ratzel at scd.magma-da.com
Sat May 22 15:23:48 EDT 2004
How about this:
>>> import re
>>> for line in open( "inputFile", "r" ).readlines():
... print re.split( "\s+", line.strip() )
...
['Name:', 'Date:', 'Time:', 'Company:', 'Employee', 'Number:']
['Jim', '2.03.04', '12:00', 'JimEnt', '4']
['Steve', '3.04.32', '03:00', 'SteveEnt', '5']
>>>
-Rick Ratzel
Uri wrote:
> I have a file that looks like this: (but longer, no wordwrap)
>
> Name: Date: Time: Company: Employee Number:
> Jim 2.03.04 12:00 JimEnt 4
> Steve 3.04.32 03:00 SteveEnt 5
>
> I want to load 'Jim' and '12:00' and those types of answers into
> variables in my program, the only delimiter in the file is whitespace.
> How do I do this?
>
> I can do it with string.split(" ",[0]) type line for a file that's
> only delimited by single spaces, but when I'm searching for white
> space, how do I do it?
>
> THanks!
More information about the Python-list
mailing list