getting words from readline

Steve Holden steve at holdenweb.com
Sun Sep 11 08:51:12 CEST 2005


Adam wrote:
> How is best to extract word strings from a 
> line = infile.readline()  
> 
> I wish to quickly check the first word of 
> each line of a text file. 
> 
> Indeed, How do I break a lineinput() line 
> into component words (separate by spaces etc) ?
> 
> Should I be looking at; 
> Re Parser Slice StringIO ? 
> 
> 
> Any help appreciated. 
> 
Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
  >>> s = "This is a line of text"
  >>> s.split()
['This', 'is', 'a', 'line', 'of', 'text']
  >>> l = s.split()
  >>> l[0]
'This'
  >>>

As you can see above, you probably don't need any complicated modules 
for this fairly simple requirement. If you are wanting to match the 
words against each other, or against some pre-exisitng list, you might 
want to reduce the input to lower-case first.

regards
  Steve
-- 
Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC             http://www.holdenweb.com/




More information about the Python-list mailing list