How to read space separated file in python?
Steve Holden
steve at holdenweb.com
Fri Nov 21 11:00:57 EST 2008
Joe Strout wrote:
> 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.)
>
Joe:
In the special case of the None first argument (the default for the
str.split() method) runs of whitespace *are* treated as single
delimiters. So line.split() is not the same as line.split(' ').
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
More information about the Python-list
mailing list