Reading a file, sans whitespace

Tim Daneliuk tundra at tundraware.com
Sat May 22 15:30:07 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!

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]


Is this what you had in mind?

-- 
----------------------------------------------------------------------------
Tim Daneliuk     tundra at tundraware.com
PGP Key:         http://www.tundraware.com/PGP/



More information about the Python-list mailing list