getting words from readline

A

Adam

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.
 
S

Steve Holden

Adam said:
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
 
A

Adam

Ouch. That is simple with .split

I must have got confused amongst
slice re etc. searching searching.

Thanks folks. Painless.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,264
Messages
2,571,315
Members
48,001
Latest member
Wesley9486

Latest Threads

Top