Reading a file, sans whitespace

Michael Geary Mike at DeleteThis.Geary.com
Sat May 22 16:25:17 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?
> >
> > THanks!

Tim Daneliuk wrote:
> Say you have read a line in the above format into variable 's'.
> Then,
>
>      l = s.split()
>
> will return a list containing each of the fields of the line as
> an entry with the whitespace stripped out.  Then,
>
> VarName = l[0]
> VarDate = l[1]
> VarTime = l[2]
> VarCo   = l[3]
> VarEmp  = l[4]

D'oh! That's much better than the regular expression solution I posted.

The regular expression split is good to know about for more complicated
patterns, but for simple whitespace splitting there's no need for it.

Thanks,

-Mike





More information about the Python-list mailing list