Reading a file, sans whitespace

Michael Geary Mike at DeleteThis.Geary.com
Sat May 22 15:20:20 EDT 2004


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?

Use a regular expression. For speed, precompile it at the beginning of your
program:

reWhitespace = re.compile( r'\s+' )

Then, split each line with:

fields = reWhitespace.split( line )

-Mike





More information about the Python-list mailing list