Splitting lines in a file
Bryan Olson
fakeaddress at nowhere.org
Sun Jul 7 20:08:44 EDT 2002
Simon Foster wrote:
| I see it, but I don't see why? What is the explanation for this? It
| seems at odds with the documentation.
You are right. The doc indicates the following should produce the same
output:
>>> print " hello there ".split()
['hello', 'there']
>>> print " hello there ".split(" ")
['', 'hello', 'there', '']
It looks like string.split() with no seperator does a string.strip()
first.
If that's not what you want, there's also re.split (with the string and
separator args in the reverse order from string.split). To get a true
split on whitespace:
>>> import re
>>> re.split(r"\s+", " hello \n\n\t there ")
['', 'hello', 'there', '']
--Bryan
More information about the Python-list
mailing list