How to read space separated file in python?

Joe Strout joe at strout.net
Fri Nov 21 10:10:58 EST 2008


On Nov 21, 2008, at 2:08 AM, Steven D'Aprano wrote:

>    a, b = line.split()

Note that in a case like this, you may want to consider using  
partition instead of split:

   a, sep, b = line.partition(' ')

This way, if there happens to be more than one space (for example,  
because the Unicode character you're mapping to happens to be a  
space), it'll still work.  It also better encodes the intention, which  
is to split only on the first space in the line, rather than on every  
space.

(It so happens I ran into exactly this issue yesterday, though my  
delimiter was a colon.)

Cheers,
- Joe




More information about the Python-list mailing list