I am unable to figure out why the first two statements work as I
expect them to and the next two do not. Namely, the first two spit the
sentence into its component words, while the latter two return the
whole sentence entact.

import string
from string import whitespace
mytext = "The quick brown fox jumped over the lazy dog.\n"

print mytext.split()
print mytext.split(' ')
print mytext.split(whitespace)
print string.split(mytext, sep=whitespace)


Split does its work on literal strings, or if a separator is not specified, on a set of data, splits on arbitrary whitespace.

For an example, try

  s = "abcdefgbcdefgh"
  s.split("c") # ['ab', 'defgb', 'defgh']
  s.split("fgb") # ['abcde', 'cdefgh']


string.whitespace is a string, so split() tries to use split on the literal whitespace, not a set of whitespace.

-tkc





--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to